I. Map set
0. The Map interface
0). An overview
java.util.Map<K,V>
- Double row set
- K cannot be repeated
- K and V correspond one by one, and their data types can be the same or different
2. Common implementation classes of Map interfaces
Common implementation classes are
HashMap<K,V>
LinkedHashMap<K,V>
HashTable<K,V>
1.HashMap<K,V>
1.)
A. Implementation of Map interface based on hash table b. Out-of-order Tips: Internal storage records are automatically sorted according to the hash value of the key. This item is invalid due to a large amount of data
C. Asynchronism, multithreading d. The key and value can be null
2). Subclasses LinkedHashMap
A. Hash table + LinkedHashSet implementation B. ordered C. Inherited HashMap, similar to the relationship between HashSet and LinkedHashSet
3). The hashCode() and equals() methods need to be overridden when storing custom objects because keys cannot be duplicated
The reason for rewriting is that if you do not rewrite it, the program will not be able to recognize that they are the same object, and the reason for rewriting is to prevent hash conflicts and different contents or the same contents with different hash values
2.HashTable<K,V>
1.)
A. Map interface implementation based on hash table B. Out-of-order C. Synchronization, single thread, thread safety, slow speed D. The key and value cannot be null e. The speed of a Vector is slow because it is single-threaded f. The subclass Properties exists because it is a unique collection associated with IO streams
Common methods of Map interfaces
1.put(K key,V value)
To return tovalue
If the key already exists, the new value is used to replace the original value
2.get(Object key)
To return tovalue
3.ContainsKey(Object key)
To return toboolean
4.ContainsValue(Object value)
To return toboolean
5. keySet()
To return toThe Set view that contains keys in the map
Get all the keys and put them in the Set, and then if you iterate through the Set you can iterate through the Map Set using get(Key)
6. entrySet()
To return toThe Set view that contains the mapping relationship in the map
1). Entry class
A. nested classes
Map.Entry
: The internal interface of the Map interface is used to:
When a Map is created, an Entry object is created in the Map, which records the mapping between keys and values
B. Mapping items (key-value pairs)
C. Contains getKey() and getValue() methods
2.) entrySet () method
Take multiple entries from the Map object and place them in the Set Set, which is then iterated through the Entry object’s getKey() and getValue()
Set<Map.Entry<String,Integer>> set = map.entrySet();
Copy the code
New features of JDK9
List interface, Set interface, Map interface added a new static method of, which can add multiple elements to the Set at one time, but is not suitable for its implementation class, such as ArrayList, etc. At the same time, the Set cannot be modified later (add elements, etc.), the added elements should compound the characteristics of the Set (Set and Map cannot add duplicate elements).