A map is used to store data that has a mapping relationship, and the map holds two sets of data: Key and value, all of which can make any reference type of data, but key cannot be duplicated. Therefore, the corresponding value can be taken out by the specified key. The map interface defines the following common methods:
1, Void Clear (): Delete the map in the key value pair.
2. Boolean containskey (Object key): Queries whether the map contains the specified key and returns True if it is included.
3. Boolean containsvalue (Object value): Queries whether the map contains the specified value and returns True if it is included.
4. Set EntrySet (): Returns the set set of key-value pairs contained in the map, each of which is a Map.entry object (entry is the inner class of the map).
5, Object get (Object key): Returns the value corresponding to the specified key, or null if the map does not contain a key.
6. Boolean isEmpty (): Query if map is empty and returns true if NULL.
7. Set KeySet (): Returns the set set of all keys in the map.
8, Object put (object Key,object value): Add a key-value pair, if there is already an identical key value, the new key-value pair overrides the old key-value pair.
9. Void Putall (map m): Copies the key-value pairs from the specified map to the map.
10, Object remove (Object key): Removes the key value pair corresponding to the specified key, returns the value that can be associated, or null if key does not exist.
11, int size (): Returns the number of key-value pairs in the map.
12. Collection VALUES (): Returns the Collection of all value components in the map.
The map contains an inner class: Entry. The class encapsulates a key-value pair, which contains three methods:
1, Object GetKey (): Returns the key value contained in the entry.
2, Object Getvaleu (): Returns the value contained in the entry.
3, Object SetValue (V value): Sets the value contained in the entry and returns the value of the new setting.
Second, HashMap and Hashtable implementation classes:
1, the difference between HashMap and Hashtable:
1) Synchronization: Hashtable is synchronous, and some methods in this class ensure that the objects in the Hashtable are thread-safe. The HashMap is asynchronous, so objects in HashMap are not thread-safe. Because the requirements of synchronization affect the efficiency of execution, it is a good choice to use HashMap if you do not need a thread-safe collection, which avoids the unnecessary performance overhead associated with synchronization, thus improving efficiency.
2) Value: HashMap allows you to use a null value as the key or value of an entry for a table, but Hashtable cannot be placed in a null value. HashMap a maximum of one key value is null, but there can be countless multiple value values of NULL.
2, Performance: HashMap performance is the best, Hashtable performance is the worst (because it is synchronous)
3. Note:
1) The object used as a key must implement the Hashcode and Equals methods.
2) The order of key-value pairs cannot be guaranteed
3) Try not to use mutable objects as their key values.
Third, Linkedhashmap:
Its parent class is HashMap, which uses a doubly linked list to maintain the order of key-value pairs, and the order of iteration is consistent with the order in which key-value pairs are inserted. Linkedhashmap need to maintain the insertion order of elements, so performance is slightly lower than hashmap, but there is good performance in iterating through the elements, because it maintains the internal order in a linked list.
Four, TreeMap:
The map interface derives a sortmap subinterface, and the Sortmap implementation class is TreeMap. TreeMap is also based on the red-black tree to sort all keys, in two ways: natural sorting and custom sorting. The TreeMap key is stored in the form of treeset, and the requirement for key is basically consistent with the requirements of the TreeSet element.
1, Map.entry firstentry (): Returns the key value pair corresponding to the minimum key, or null if Map is empty.
2, Object Firstkey (): Returns the minimum key, or null if it is empty.
3, Map.entry lastentry (): Returns the key value pair corresponding to the maximum key, or null if Map is empty.
4, Object Lastkey (): Returns the maximum key, or null if it is empty.
5. Map.entry higherentry (Object key): Returns a key-value pair that is located after key, or null if it is empty.
6. Map.entry lowerentry (Object key): Returns a key-value pair that is located before key, or null if it is empty.
7, Object Lowerkey (Object key): Returns a key value in front of key, or null if NULL.
8, Navigablemap subMap (Object fromkey,boolean fromlnclusive,object tokey,boolean toinciusive): Returns the map's child map, Its key range is from Fromkey to Tokey.
9, Sortmap SubMap (Object fromkey,object Tokey); Returns the map's child map, whose key range is from Fromkey (including) to Tokey (not included).
10, Sortmap Tailmap (Object Fromkey, Boolean inclusive): Returns the map's child map whose key range is greater than fromkey (whether it includes all keys depending on the second parameter).
11, Sortmap Headmap (Object Tokey, Boolean inclusive): Returns the map's child map whose key range is less than tokey (whether it includes all keys depending on the second parameter).
Five, Weakhashmap:
Weakhashmap and HashMap are basically the same, except that the latter's key retains a strong reference to the object, that is, as long as the HashMap object is not destroyed, the object referenced by all key objects is not garbage collected. HashMap also does not automatically delete the key-value pair objects for these keys. However, the objects referenced by the Weakhashmap key are not referenced by other strongly referenced variables, and the objects referenced by these keys may be recycled. Each key object in the Weakhashmap holds a weak reference to the actual object, and when the actual object corresponding to the key is reclaimed, Weakhashmap automatically deletes the key-value pair for that key.
Six, Identityhashmap Category:
Identityhashmap is basically similar to HashMap, except that when two keys are strictly equal, that is, Key1==key2, it considers the two keys to be equal. Identityhashmap also allows NULL, but does not guarantee the order of key-value pairs.
Seven, Enummap Category:
1. All keys in Enummap must be enumerated values of a single enumeration class, and the enumeration class must be displayed or implicitly specified when creating Enummap.
2. Enummap maintains the order of key-value pairs according to the natural order of key, that is, the order in which the enumeration values are defined in the enumeration class.
3. Enummap is not allowed to use NULL as the key value, but value can.
How several maps in Java are used, and briefly explain why and why