Java Hashmap,treemap and Linkedhashmap of the detailed _java

Source: Internet
Author: User

A detailed explanation of Java Hashmap,treemap and Linkedhashmap

This morning when the interview asked Java,map related things, I remember the HashMap and TREEMAP related content, come back hurriedly tried a few demo understand

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, the bottom uses the form of key-value pairs to store data map.entry is a child of the internal, map of different implementations of the key value pairs of the index scheme is different
HashMap itself is indexed by the hash function. We can't determine the order of the last key values.

But there is an interesting phenomenon is that in the integer as the key value pair, when the number of digits is 1-bit when the key values are from small to large row, the number of digits up to two-bit when the problem may exist

There is a balance tree inside the TREEMAP to store the index of the key value, TreeMap to sort the key values according to the comparison function, I speculate that there may be a avltree inside

Linkedhashmap This exists an attribute is that the key value pairs are sorted in the order of insertion, and if there is a repeat insertion in the order of the first insertion, one of the online claims is that there are 2 heavy hashes inside the structure.

A solution to the order problem, a solution to the storage problem, correctness to be confirmed

HashMap and TreeMap are the two most commonly used map structures, in general hashmap efficiency is higher, and most common, if we need to order the key value, we will use the TreeMap

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.