map and other collections

3 minute read

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:

    1. HashMap<K,V>

      (1) The bottom layer is a hash table (fast query speed)

      After JDK1.8+, array + red-black tree

      (2) Unordered collection

    2. 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
    1. public V put(K key,V value): add the specified key and specified value to the Map collection
    2. public V remove(Object key): remove the specified key and specified value from the Map collection
    3. public V get(Object key): Find the value based on the key
    4. boolean containsKey(Object key): Determine whether the set contains the specified key
    5. public Set<K> keySet(): Get all the keys of the Map and store them in the Set collection
    6. public 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 value

    Set<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:

    1. It is not allowed to store null (empty)
    2. The earliest two-column set is synchronous
    3. 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:

    1. The of method is only applicable to the List interface, Set interface, Map interface, not applicable to the implementation class
    2. 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.
    3. The Set interface and Map interface cannot have duplicate elements when calling the of method, otherwise an IllegalArgumentException will be thrown