Details of java HashMap TreeMap and LinkedHashMap

  • 2020-05-19 04:48:51
  • OfStack

Details of java HashMap,TreeMap and LinkedHashMap

During the interview this morning, I asked about Java and Map, but I forgot about HashMap and TreeMap, so I tried several demo to understand them


package Map; 
 
import java.util.*; 
 
public class HashMaps { 
  public static void main(String[] args) { 
    Map map = new HashMap(); 
    map.put("a", "aaa"); 
    map.put("b", "bbb"); 
    map.put("c", "ccc"); 
    map.put("d", "ddd"); 
 
    Iterator iterator = map.keySet().iterator(); 
    while (iterator.hasNext()) { 
      Object key = iterator.next(); 
      System.out.println("map.get(key) is :" + map.get(key)); 
    } 
 
    Hashtable tab = new Hashtable(); 
    tab.put("a", "aaa"); 
    tab.put("b", "bbb"); 
    tab.put("c", "ccc"); 
    tab.put("d", "ddd"); 
    Iterator iterator_1 = tab.keySet().iterator(); 
    while (iterator_1.hasNext()) { 
      Object key = iterator_1.next(); 
      System.out.println("tab.get(key) is :" + tab.get(key)); 
    } 
 
    TreeMap tmp = new TreeMap(); 
    tmp.put("a", "aaa"); 
    tmp.put("b", "bbb"); 
    tmp.put("c", "ccc"); 
    tmp.put("d", "ddd"); 
    tmp.put("a", "aba"); 
    Iterator iterator_2 = tmp.keySet().iterator(); 
    while (iterator_2.hasNext()) { 
      Object key = iterator_2.next(); 
      System.out.println("tmp.get(key) is :" + tmp.get(key)); 
    } 
     
     
    LinkedHashMap<String ,Integer> linkedHashMap = new LinkedHashMap<String,Integer>(); 
    linkedHashMap.put("dasdsa", 1); 
    linkedHashMap.put("gdsf",2); 
    linkedHashMap.put("texvdfd", 3); 
    linkedHashMap.put("bdada", 4); 
     
    linkedHashMap.put("gdsf",3); 
    for(String temp : linkedHashMap.keySet()){ 
      System.out.println(temp); 
    } 
     
  } 
 
} 

Map is different from List in that the underlying layer stores data in the form of key-value pairs. Map.Entry is an internal sub-entry, and different implementations of Map have different indexing schemes for key-value pairs
HashMap itself USES the hash function to index the key values and we can't determine the order of the last key values

However, there is an interesting phenomenon that when Integer is used as the key value pair, when the digit is 1, the key value is arranged from small to large, and when the digit rises to two, there may be a problem

There is a balance tree inside TreeMap to store the key value index, and TreeMap sorts the key value according to the comparison function. I speculate that there may be 1 AVLtree inside

One of the features of LinkedHashMap is that the key and value pairs are sorted according to the insertion order. If there are repeated inserts, remember the order of the first insertion. One of the online claims is that there are two duplicate hash in the structure

One solves the order problem, one solves the storage problem, and the correctness is to be confirmed

HashMap and TreeMap are the two most commonly used Map structures. Generally speaking, HashMap is efficient and the most common. TreeMap is only used if we need to order the key values

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: