1.Map collection:
- java.util.Map <k, v> package
- A double row collection element contains two values (key, value)
- and key value data type may be the same or different
- do not allow duplicate key, value allowed to repeat
- key and value one correspondence
Map popular sub-categories:
- HashMap: achieve Map <k, v> interfaces, multi-threaded
- Before JDK1.8 + singly linked using the array, followed by a one-way linked list arrays + / red-black trees (array length of more than 8 using the red-black tree), to improve the speed of the query
- Unordered set of storage elements and element order may differ removed
- LinkedHashMap: Inheritance HashMap
- The bottom layer is: + chain hash table (to ensure that the iteration sequence)
- Ordered set, store and retrieve the order of elements has been
Hashtable: realized Map <k, v> interfaces, single-threaded
- JDK1.0 there, earlier collections
- Key or value is not null
- Single-threaded speed is slow, has been replaced HashMap
- Subclass Properties collection:
-
-
- It is still widely used
- Unique combination of flow and a set of IO
-
Map common method ::
- public V put (K key, V value): Specifies the key and value added element to the Map
- public V remove (Object key): delete the specified key elements, deleting element values is returned
- public V get (Object key): the specified key, obtain a corresponding value
- boolean containsKey (Object key): Map element is determined whether the specified key comprising
- public Set <K> keySet (): Gets Map collection of all key, set into the Set
- pubic Set <Map.Entry <K, V >> entrySet (): get all Map collection of key elements of the collection into the Set
Note:
Map.Entry <K, V>: Entry has an internal interface in the Map interface, when creating a collection Map, creates an internal Entry object for recording the object key and value (stored mapping between keys and values)
1 public class demo03 { 2 3 public static void main(String[] args) { 4 5 Map<String, String> map = new HashMap<>(); 6 7 //1.存入键值对 8 map.put("key1", "value1"); 9 map.put("key2", "value2"); 10 map.put("key3", "value3"); 11 map.put("key4", "value4"); 12 13 System.out.println(map); 2. Delete the corresponding key value pairs//1514 16 map.remove ( "key1" ); . 17 System.out.println (Map); 18 is . 19 // 3. value obtained corresponding to the value of the key 20 is System.out.println (as map.get ( "key3" )); 21 is 22 // key 4 determines whether or not the key corresponding to comprise 23 is System.out.println (map.containsKey ( "^ key5" )); 24 25 @ 5. the traverse all the Map key, a method 26 is the Set <String > SET = map.keySet (); 27 28 the Iterator <String> Iterator = set.iterator (); 29 the while (iterator.hasNext ()) { 30 System.out.println (Iterator.next ()); 31 is } 32 33 is // iterate Map all key, second method 34 is // for (String S: map.keySet () 35 // ) { 36 // the System .out.println (S); 37 [ // } 38 is 39 // 6. the traverse all keys Map object, a method 40 the Set <of Map.Entry <String, String >> SET2 = EnumMap.entrySet (); 41 is 42 is the Iterator <of Map.Entry <String, String >> a iterator2 = set2.iterator (); 43 is the while (iterator2.hasNext ()) { 44 is System.out.println (iterator2.next ()); 45 } 46 is 47 // iterate all keys Map object, two methods 48 // for (of Map.Entry <String, String> entry: EnumMap.entrySet ( ) 49 // ) { 50 // System.out.println (entry); 51 is // } 52 is 53 is } 54 is }
Use HashMap to store custom type:
- If the custom type as a key, and must override the equals method hashcode method is used to ensure unique key
- If the Java native types such as String when the key is not because String has rewritten these two methods
1 public class demo04 { 2 3 public static void main(String[] args) { 4 5 HashMap<Person,String> hashMap = new HashMap<>(); 6 7 hashMap.put(new Person("wzh",24),"person1"); 8 hashMap.put(new Person("wzh",24),"person2"); 9 hashMap.put(new Person("wxl",23),"person3"); 10 hashMap.put(new Person("wzl",23),"person4"); 11 12 for (Map.Entry<Person,String> entry: hashMap.entrySet() 13 ) { 14 System.out.println(entry); 15 } 16 17 /** 18 * 输出 19 * Person{name='wzh', age=24}=person2 20 * Person{name='wzl', age=23}=person4 21 * Person{name='wxl', age=23}=person3 22 */ 23 } 24 }
Use LinkedHashMap: the same access sequence
1 public class demo05 { 2 3 public static void main(String[] args) { 4 5 HashMap<String,String> hashMap = new HashMap<>(); 6 7 hashMap.put("key3","value3"); 8 hashMap.put("key2","value2"); 9 hashMap.put("key4","value4"); 10 hashMap.put("key1","value1"); 11 12 //存取顺序不相同 13 System.out.println(hashMap); //{key1=value1, key2=value2, key3=value3, key4=value4} 14 15 LinkedHashMap<String,String> linkedHashMap = new LinkedHashMap<>(); 16 17 linkedHashMap.put("key3","value3"); 18 linkedHashMap.put("key2","value2"); 19 linkedHashMap.put("key4","value4"); 20 linkedHashMap.put("key1","value1"); 21 22 //存取顺序相同 23 System.out.println(linkedHashMap); //{key3=value3, key2=value2, key4=value4, key1=value1} 24 } 25 }
New features of 2.JDK9
New Method of the collection:
- List interface, Set Interface, Map of the interface to increase a static method, a disposable set for adding a plurality of elements, does not apply to the interface implementation class
- Use of the method can no longer add elements in the collection, you can not call add, put methods otherwise it will throw an exception
- For the Set interface and use of the Map interface when the method, otherwise it will not have duplicate elements thrown