Source code analysis of java Collection classes: Map (2), javamap

Source: Internet
Author: User

Source code analysis of java Collection classes: Map (2), javamap

This section mainly discusses the differences and usage of Several implementation classes of the Map interface.

1. Thread Security

Hashtable is thread-safe (similar to Stringbuffer and Vector), but not others. As for why, read some previous articles, which have been described in detail earlier.

2. Applicable Conditions

HashMap: applicableQuick SearchAndUnordered dataIn this case, the allowed key is null (only once), and the allowed value is null;

TreeMap: applicableOrdered StorageThe key cannot be empty;

Hashtable: It is applicable to ensure synchronizationThread SecurityThe key and value cannot be empty;

LinkedHashMap: saves ElementsInsert sequenceIt has all the features of HashMap, but the time duration is slower than that of HashMap.

Examples of comparing HashMap and LinkedHashMap:

1 private static void linkedHashMapAnal () {2 // LinkedHashMap has all the features of HashMap, and can retain the sequence of elements inserted. 3 Map <String, Integer> linkedHashMap = new LinkedHashMap <String, integer> (); 4 Map <String, Integer> hashMap = new HashMap <String, Integer> (); 5 6 hashMap. put ("A", 120); 7 hashMap. put ("B", 100); 8 hashMap. put ("C", 105); 9 hashMap. put ("D", 200); 10 for (String key: hashMap. keySet () {11 System. out. print (key + ":" + hashMap. get (key) + ""); 12} // D: 200 A: 120 B: 100 C: 10513 14 System. out. println (); 15 linkedHashMap. put ("A", 120); 16 linkedHashMap. put ("B", 100); 17 linkedHashMap. put ("C", 105); 18 linkedHashMap. put ("D", 200); 19 for (String key: linkedHashMap. keySet () {20 System. out. print (key + ":" + linkedHashMap. get (key) + ""); 21} // A: 120 B: 100 C: 105 D: 200 22}

 

3. sorting problems

Map sets are sorted by key and by value.

  • Sort by key

There are two ways to sort Map by key: the comparator in TreeMap and the sort method in the Collections class.

1. Sort TreeMap by key

1 // use the default comparator to sort key values in ascending order and store 2 Map <String, Integer> treeMap = new TreeMap <String, Integer> (); 3 treeMap. put ("B", 120); 4 treeMap. put ("C", 105); 5 treeMap. put ("A", 100); 6 for (String key: treeMap. keySet () {7 System. out. print (key + ":" + treeMap. get (key) + ","); 8} // A: 100, B: 120, C: 105, 9}
1 // custom Comparator for storing 2 Map <String, Integer> treeMap = new TreeMap <String, Integer> (new Comparator <String> () in descending order () {3 @ Override 4 public int compare (String o1, String o2) {5 // return 0; 6 return o2.compareTo (o1) by default ); // in descending order 7} 8}); 9 treeMap. put ("B", 120); 10 treeMap. put ("C", 105); 11 treeMap. put ("A", 100); 12 for (String key: treeMap. keySet () {13 System. out. print (key + ":" + treeMap. get (key) + ","); 14} // C: 105, B: 120, A: 100,
1 // use a subset of SortedMap to initialize the TreeMap object. Call the comparator that comes with the parameter. 2 // Note: The parameter must be an implementation class of the SortedMap interface. Otherwise, the default comparator 3 SortedMap <String, integer> subMap = new TreeMap <String, Integer> (new Comparator <String> () {4 5 @ Override 6 public int compare (String o1, String o2) {7 // TODO Auto-generated method stub 8 return o2.compareTo (o1); 9} 10}); 11 subMap. put ("B", 100); 12 subMap. put ("F", 120); 13 subMap. put ("D", 105); 14 for (String key: subMap. keySet () {15 System. out. print (key + ":" + subMap. get (key) + ","); 16} // F: 120, D: 105, B: 100, 17 18 System. out. println (); 19 20 Map <String, Integer> treeMap = new TreeMap <String, Integer> (subMap); 21 for (String key: treeMap. keySet () {22 System. out. print (key + ":" + treeMap. get (key) + ","); 23} // F: 120, D: 105, B: 100, 24 25 System. out. println (); 26 27 treeMap. put ("A", 200); 28 treeMap. put ("C", 300); 29 treeMap. put ("E", 400); 30 for (String key: treeMap. keySet () {31 System. out. print (key + ":" + treeMap. get (key) + ","); 32} // F: 120, E: 400, D: 105, C: 300, B: 100, A: 200,

2. Sort HashMap by key

1 // use the HashMap subset to initialize the TreeMap object and call the default comparator (ascending). 2 // The parameter here is the implementation class of the Map interface. 3 Map <String, integer> subMap = new HashMap <String, Integer> (); 4 subMap. put ("B", 100); 5 subMap. put ("A", 120); 6 subMap. put ("C", 105); 7 subMap. put ("D", 200); 8 for (String key: subMap. keySet () {9 System. out. print (key + ":" + subMap. get (key) + ","); 10} // D: 200, A: 120, B: 100, C:, 11 12 System. out. println (); 13 14 Map <String, Integer> treeMap = new TreeMap <String, Integer> (subMap); 15 for (String key: treeMap. keySet () {16 System. out. print (key + ":" + treeMap. get (key) + ","); 17} // A: 120, B: 100, C: 105, D: 200,
1 Map <String, Integer> hashMap = new HashMap <String, Integer> (); 2 3 // hashMap is arranged by the hash value of the key by default, which is a type of unordered storage 4 hashMap. put ("A", 100); 5 hashMap. put ("B", 120); 6 hashMap. put ("C", 105); 7 hashMap. put ("D", 100); 8 for (String key: hashMap. keySet () {9 System. out. print (key + ":" + hashMap. get (key) + ""); 10} // D: 100 A: 100 B: 120 C: 105 11 12 System. out. println (); 13 // store the HashMap object to 14 ArrayList in the List set <Map. entry <String, Integer> mapList = new ArrayList <Map. entry <String, Integer> (hashMap. entrySet (); 15 // call the sort method of Collections 16 Collections. sort (mapList, new Comparator <Entry <String, Integer> () {17 18 @ Override19 public int compare (Entry <String, Integer> o1, Entry <String, integer> o2) {20 // TODO Auto-generated method stub21 return o1.getKey (). compareTo (o2.getKey (); // 22 in ascending order // return o2.getKey (). compareTo (o1.getKey (); // 23} 24} in descending order); 25 26 for (int I = 0; I <mapList. size (); I ++) {27 System. out. print (mapList. get (I ). getKey () + ":" + mapList. get (I ). getValue () + ""); 28} // A: 100 B: 120 C: 105 D: 100
  • Sort by value

With the above example, we know that we can also sort the values of Map sets through the sort method of Collections, including HashMap and TreeMap.

1 Map <String, Integer> hashMap = new HashMap <String, Integer> (); 2 3 hashMap. put ("A", 100); 4 hashMap. put ("B", 120); 5 hashMap. put ("C", 105); 6 hashMap. put ("D", 110); 7 for (String key: hashMap. keySet () {8 System. out. print (key + ":" + hashMap. get (key) + ""); 9} // D: 110 A: 100 B: 120 C: 105 10 11 System. out. println (); 12 // store the HashMap object in the List set. 13 ArrayList <Map. entry <String, Integer> mapList = new ArrayList <Map. entry <String, Integer> (hashMap. entrySet (); 14 // call Collections's sort method 15 Collections. sort (mapList, new Comparator <Entry <String, Integer> () {16 17 @ Override18 public int compare (Entry <String, Integer> o1, Entry <String, integer> o2) {19 // TODO Auto-generated method stub20 return o1.getValue (). compareTo (o2.getValue (); // sort by value in ascending order. You only need to change getKey to getValue to 21 // return o2.getValue (). compareTo (o1.getValue (); // sort by value in descending order. You only need to replace getKey with getValue to 22} 23}); 24 25 for (int I = 0; I <mapList. size (); I ++) {26 System. out. print (mapList. get (I ). getKey () + ":" + mapList. get (I ). getValue () + ""); 27} // A: 100 C: 105 D: 110 B: 120
1 ArrayList <Map. entry <String, Integer> mapList = new ArrayList <Map. entry <String, Integer> (treeMap. entrySet (); 2 Collections. sort (mapList, new Comparator <Entry <String, Integer> () {3 4 @ Override 5 public int compare (Entry <String, Integer> o1, Entry <String, integer> o2) {6 // TODO Auto-generated method stub 7 return o1.getValue (). compareTo (o2.getValue (); // 8 in ascending order // return o2.getValue (). compareTo (o2.getValue (); // 9} 10} in descending order); 11 for (int I = 0; I <mapList. size (); I ++) {12 System. out. print (mapList. get (I ). getKey () + ":" + mapList. get (I ). getValue () + ""); 13} // B: 100 D: 105 F: 120 C: 300 E: 400 A: 500 14 15}

 

Related Article

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.