The Map interface
A Map is used to store mapped data. A Map contains two sets of values, one for the key of the Map and the other for the value of the Map.
A Map collection is used to store pairs of elements (that is, a pair of key values) that are mapped by key to its key value value.
-- - | the Map interface Map the key to value object. A map cannot contain duplicate keys; Each key can map to at most one value. -- - | a HashMap USES hash table implementation, so disorderly - | TreeMap can prioritize - health | Hashtable: is the underlying hash table data structure, the thread is synchronous, may not be deposited in the null keys, a null value. Less efficient, replaced by HashMap. -- - | a HashMap: is the underlying hash table data structure, the thread is out of sync, can be deposited in the null keys, a null value. To ensure that keys are unique, you need to override the hashCode method and equals method. -- - | LinkedHashMap: the subclasses based on hash table and into the linked list. You can add and delete maps to improve efficiency. -- - | a TreeMap: the underlying is a binary tree data structure. You can sort the keys in the Map collection. You need to use Comparable or Comparator for comparison sorting. Return 0 to determine the uniqueness of the key.Copy the code
Common methods:
1, add: 1, V put(K key, V value) (can be the same key, but the added value will overwrite the previous value, if not null) 2, putAll(Map<? extends K,? Extends V> m) copies all mapping relationships from the specified mapping into this mapping (optional operation). Delete 1, remove() delete the associated object, specify the key object 2, clear() empty the collection object 3, obtain 1: value get(key); Can be used to determine whether a key exists. If the specified key does not exist, null is returned. 3. Judgment: Boolean containsKey(Object key) Checks whether the set contains the specified key. 3. Boolean containsValue(Object 4, Length: Int size ()Copy the code
How to traverse a Map:
1. Remove all keys from map set and store them in set set. Set<K> keySet() returns the Set of all key objects and uses the get method to get the corresponding value of the key. 2, values(), get all values. Map.entry It is recommended to use Set< map.entry <k, V> > entrySet() to package the key-value mappings in the Map Set into an object The map. Entry object obtains its key and value through the getKey and getValue of the map. Entry object.Copy the code
public static void main(String[] args) { Map<String,String> map = new HashMap<String,String>(); The map. The put (" zs ", "* *"); The map. The put (" wh ", "detective"); The map. The put (" sz ", "array"); The map. The put (" zs ", "zhang" "); Set<Map.Entry<String,String>> set=map.entrySet(); Iterator<Map.Entry<String,String>> iter =set.iterator(); while (iter.hasNext()){ Map.Entry<String, String> me =iter.next(); System.out.println(me.getKey()+"-->"+me.getValue()); }}Copy the code
HashMap
The underlying hash table data structure, threads are not synchronized, you can store null keys, NULL values. To ensure that keys are unique, you need to override the hashCode method and equals method. Before JDK1.8, hash table is implemented by array + linked list. That is, the linked list is used to handle conflicts, and the linked list with the same hash value is stored in a linked list. However, when there are many elements in a bucket, that is, there are many elements with the same hash value, searching by key value is inefficient. In JDK1.8, hash table storage is realized by array + linked list + red-black tree. When the length of the linked list exceeds the threshold value (8), the linked list is converted to red-black tree, which greatly reduces the search time. In simple terms, hash tables are implemented by an array + a linked list + a red-black tree (red-black tree has been added to JDK1.8), as shown below.
TreeMap
At the bottom is a binary tree (red-black tree) data structure. Features: Threads are not synchronized, and keys in the Map collection can be sorted at the same time
The specific implementation
package work.february.one; import java.util.HashMap; import java.util.Map; import java.util.Set; public class Demo9 { public static void main(String[] args) { Map<Integer,String> map =new HashMap<Integer,String>(); // Add the element map.put(1," zhang SAN ") to the set; // return the old element map.put(1," triple A"); Map. The put (3, "detective"); The map. The put (2, "zhao four"); Map. The put (4, "bill"); Key Boolean flag = map.containsKey(1); Key Boolean flag1 = map.containsValue(" flag1 "); // Find the corresponding value map.get(2); // determine whether map.isempty () isEmpty; Map.remove (3); // Set<Integer> Set = map.keyset (); for (Integer s:set) { System.out.println(s+"->"+map.get(s)); }}}Copy the code
Time to talk:
If the guest officer feel edible appropriate can give a free praise! Thanks, thanks! Go slow, guest officer! I suggest you pack it and come back next time. Shopkeeper QQ: 309021573, welcome harassment!