Map Collection and exist side by side. For storing data having a mapping relationship: Key-Value
- Map of the key and value can be any type of reference data
- Map of the key with Set to store, not allowed to repeat, that is the same, common String class as the Map "key"
- There is a one-way one to one relationship between the key and value, that is, always find unique value determined by the specified key
1.HashMap
HashMap is not thread safe
package com.gather; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; public class HashMapPractise { public static void main(String[] args) { Map<String, Person> map = new HashMap<>(); Person p1 = new Person("张三", 22); Person p2 = new Person("李四", 23); Person p3 = new Person("王五", 22); map.put ( "AA" , P1); map.put ( "BB" , P2); map.put ( "the CC" , P3); // first traversal: can be taken out simultaneously on the key for (String STR: map.keySet ()) { System.out.println ( "bond is:" + str + ", the value:" + as map.get (STR)); } System.out.println ( "----- ------------------------- " ); // The second way to navigate: just take the keys or only the values for (String str: the Map. keySet ()) { System.out.println ( "bond is:" + STR); } for (the Person Person: map.values ()) { the System.out.the println ( "value:" +the Person); } System.out.println ( "------------------------------" ); // Recommended: Third species traversal: can be taken out simultaneously on the key for (the entry <String, the Person> entry: EnumMap.entrySet ()) { System.out.println ( "bond is:" + entry.getKey () + " , the value of: "+ entry.getValue ()); } System.out.println ( " ------------------------------ " ); // fourth traversal: key-value pair can be taken out simultaneously the Iterator <the Entry <String, the Person entries It >> = . EnumMap.entrySet () Iterator (); the while (entries.hasNext ()) { the Entry <String, the Person> = entry entries.next (); System.out.println ( "bond is:" + entry.getKey () + " , the value:" + entry.getValue ()); } } }
2.Hashtable
Hashtable is thread safe
package com.gather; import java.util.Enumeration; import java.util.Hashtable; import java.util.Map; import java.util.Map.Entry; public class HashtablePractise { public static void main(String[] args) { Map<String, Person> map = new Hashtable<>(); Person p1 = new Person("张三", 22); Person p2 = new Person("李四", 23); map.put("AA", p1); map.put("BB", p2); for (Entry<String, Person> entry : map.entrySet()) { System.out.println("键为:" + entry.getKey() + ",值为:" + entry.getValue()); } System.out.println("------------------------------"); Hashtable<String, Person> hashtable = new Hashtable<>(); hashtable.put("AA", p1); hashtable.put("BB", p2); // Hashtable的另外一种遍历方式:Enumeration Enumeration<String> enuKey = hashtable.keys(); while (enuKey.hasMoreElements()) { System.out.println(enuKey.nextElement()); } Enumeration<Person> enuValue = hashtable.elements(); while (enuValue.hasMoreElements()) { System.out.println(enuValue.nextElement()); } } }