Set and map list in Java

Source: Internet
Author: User
Java collections are mainly divided into three types: Map (ing) Set (SET) List (list) 1. Map Map is the ing from key to value, Duplicate keys are not allowed. Each key can map up to one value. The map interface has four implementation classes: Hashmap  Hashtable linkedhashmap and Treemap HashmapHash Table Implementation Is the best full implementation of the map interface. Keys and values can be empty and non-multi-thread synchronization (Insecure). Therefore, hashmap needs to be manually synchronized in multi-thread scenarios and traversed using iterator. Key and value can be empty: Hashmap allows null values (both key and value ). Hashmap allows a null value to indicate that the value corresponding to one or more keys is null. When the get () method returns a null value, it can indicate that the key does not exist in hashmap or that the corresponding value of the key is null. Therefore, the get () method cannot be used to determine whether a key exists in the hashmap. Instead, the containskey () method should be used to determine whether a key exists in the hashmap. Non-multithreading synchronization: Hashmap does not support thread synchronization. At any time, multiple threads can write hashmap at the same time, which may cause data inconsistency. If synchronization is required, you can use the synchronizedmap method of collections to synchronize hashmap, or use concurrenthashmap, for example, map synmap = collections. synchronizedmap (New hashmap (...));  Hashtable  Similar to hashmap, It inherits from the dictionary class. The difference is that it does not allow null keys or values to be recorded. It supports thread synchronization, that is, only one thread can write hashtable at any time, as a result, hashtable is slow in writing, and hashtable and hashmap both use iterator. For historical reasons, hashtable also uses the enumeration method. Enumeration is not recommended now, not only because enumeration does not provide a way to safely delete elements in hashtable when traversing keys or values; In addition, the enumeration name does not comply with Sun's Java Naming Conventions (enumeration is more appropriate ). to be compatible with the two, hashtable has to provide the enumeration internal class that implements both enumeration and iterator interfaces. we recommend that you use hashmap. Linkedhashmap The record insertion sequence is saved. When iterator is used to traverse the linkedhashmap, the first record is certainly inserted first. You can also use parameters to sort the records by the number of times of application during construction. It is slower than hashmap during traversal, but in some cases, when the hashmap capacity is large and the actual data is small, the traversal may be slower than linkedhashmap, because the traversal speed of linkedhashmap is only related to the actual data and has nothing to do with the capacity, and the traversal speed of hashmap is related to its capacity. TreemapThe map sub-interface sortedmap is implemented, and the red/black tree is used as the underlying storage structure, Provides MAP storage by key. 2. Set The Set interface mainly implements two implementation classes: hashset class according to the hash Algorithm To access objects in a set, the access speed is relatively fast. treeset: The treeset class implements the sortedset interface, which can sort objects in the set. The underlying data structure of hastset is a hash table, the advantage of a hash table is that the query speed is fast (except in the worst case ). Differences between hashset and hashmap 1. hashset is implemented using hashmap at the underlying layer. 2. The key of hashmap is the object put into hashset, and the value is of the object type. 3. when the hashset add method is called, a row (key-Value Pair) is actually added to the hashmap. The key of the row is the object added to the hashset, the value of this row is a constant of the object type. 3. List The feature of list is that its elements are stored linearly, and repeated objects can be stored in the collection. . The main implementation classes of the List interface include arraylist (), which indicates that the length can be changed to an array. Random Access to elements is allowed, and it is slow to insert and delete elements to and from arraylist. Linked List (): uses the Linked List Data Structure in implementation. Fast insertion and deletion, and slow access. For random access to a list, only random elements are retrieved at a specific location. The get (INT index) method of list puts back the object at the index location specified by the index parameter in the set. The subscript starts from "0.

// Unordered output map of hashmap <string, string> HS = new hashmap <string, string> (); HS. put ("hashmap1", "hashmap"); HS. put (null, "hashmap2"); HS. put ("hashmap3", "hashmap1"); HS. put ("hashmap4", "hashmap1"); HS. put ("hashmap5", null); iterator <entry <string, string> is = HS. entryset (). iterator (); While (is. hasnext () {entry <string, string> ent = is. next (); system. out. println (ENT. getkey () + "--" + ent. getvalue () ;}// hashtable unordered output Map <string, string> table = new hashtable <string, string> (); table. put ("table6", "table"); table. put ("Table2", "Table1"); table. put ("Table1", "Table2"); table. put ("table4", "table3"); For (iterator <entry <string, string> iterator = table. entryset (). iterator (); iterator. hasnext ();) {entry <string, string> type = iterator. next (); system. out. println (type. getkey () + "--" + type. getvalue ();} // Ordered Tree output Map <string, string> temap = new treemap <string, string> (); temap. put ("3", "temap3"); temap. put ("1", "temap1"); temap. put ("2", "temap2"); temap. put ("B", "temapb"); temap. put ("A", "temapa"); iterator <entry <string, string> tree = temap. entryset (). iterator (); While (tree. hasnext () {entry <string, string> E = tree. next (); system. out. println (E. getkey () + "--" + E. getvalue ();} // linkedhashmap outputs Map <string, string> lhsmap = new linkedhashmap <string, string> (); lhsmap. put ("2", "linkedhashmap3"); lhsmap. put ("1", "linkedhashmap1"); lhsmap. put ("3", "linkedhashmap2"); lhsmap. put ("B", "linkedhashmapb"); lhsmap. put ("A", "linkedhashmapa"); iterator <entry <string, string> iterator = lhsmap. entryset (). iterator (); While (iterator. hasnext () {map. entry <string, string> E = signature. next (); system. out. println (E. getkey () + "--" + E. getvalue ());}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.