map and other collections
Published:
Java basics – map and other collections
Map and Other Collections
Map collection
java.util.Map is a two-column collection
The collection in the Collection, the elements exist in isolation
The set in the Map, the elements exist in pairs, each element is composed of two parts, a key and a value, and the corresponding value can be found through the key
Features: 1. For the elements in the Map collection, the data types of key and value can be the same or different 2. For the elements in the Map collection, the key is not allowed to be repeated, and the value can be repeated 3. Elements in the Map collection, key and value are one-to-one correspondence
Common implementation classes of Map collection:
HashMap<K,V>
(1) The bottom layer is a hash table (fast query speed)
After JDK1.8+, array + red-black tree
(2) Unordered collection
LinkedHashMap<K,V>
(1) It is a subclass of HashMap
(2) The bottom layer is a hash table + linked list, which is an ordered collection
- Common methods in the Map interface
public V put(K key,V value)
: add the specified key and specified value to the Map collectionpublic V remove(Object key)
: remove the specified key and specified value from the Map collectionpublic V get(Object key)
: Find the value based on the keyboolean containsKey(Object key)
: Determine whether the set contains the specified keypublic Set<K> keySet()
: Get all the keys of the Map and store them in the Set collectionpublic Set<Map.Entry<K,V>> entrySet()
: Get the collection of all key-value pairs of Map and store it in the Set collection
Traverse the Map collection
Set<String> set=map.ketSet(); Iterator<String> it=set.iterator(); while(it.hasNext()){ String key=it.next(); Integer value=map.get(key); System.out.println(key+":"+value); }
Entry key-value pair object
There is an internal interface Entry in the Map interface to record keys and values
getKey()
gets the key,getValue()
gets the valueSet<Map.Entry<String,Integer>> set=map.entrySet(); Iterator<Map.Entry<String,Integer>> it=set.iterator(); while(it.hasNext()){ Map.Entry<String,Integer> entry=it.next(); String key=entry.getKey(); Integer value=entry.getValue(); System.out.println(key+":"+value); }
HashMap
Map guarantees that the key is unique: the element as the key must override the hashCode method and the equals method
Not synchronized
LinkedHashMap
Inherited HashMap, the bottom layer is hash table + linked list, not synchronized
Hashtable
Features:
- It is not allowed to store null (empty)
- The earliest two-column set is synchronous
- Replaced by more advanced collections after JDK1.2+
Supplementary knowledge points
JDK1.9 added optimization to collection
A static method
of
is added to the List interface, Set interface, and Map interface to add multiple elements at once.Such as:
List<String> list=List.of("a","b","c","d");
Prerequisite for use: used when the number of elements stored in the collection is determined
Precautions:
- The of method is only applicable to the List interface, Set interface, Map interface, not applicable to the implementation class
- The return value of the of method is an immutable collection. You can no longer call add, put and other methods, otherwise UnsupportedOperationException will be thrown.
- The Set interface and Map interface cannot have duplicate elements when calling the of method, otherwise an IllegalArgumentException will be thrown