Show Me the code!
Commonly used map has treemap,hashmap,hashtable,linkhashmap, the following code on the above four are compared and simple description of its underlying implementation.
Public classMyMap { Public Static voidTestaddtreemap () {TreeMap<String,Object> TreeMap =NewTreemap<string, object>(); Treemap.put ("Zhao Yun", 23); Treemap.put ("Caocao", 24); Treemap.put ("Ma Chao", 25); Treemap.put ("Sima Yi", 26); Treemap.put ("2", 0); Treemap.put ("1", 0); Treemap.put ("3",NULL); for(Map.entry<string, object>Entry:treeMap.entrySet ()) {System.out.println (Entry.getkey ()+ ": " +Entry.getvalue ()); } } Public Static voidTesthashmap () {Map<string, object> HashMap =NewHashmap<string, object>(); Hashmap.put ("Zhao Yun", 23); Hashmap.put ("Caocao", 24); Hashmap.put ("Ma Chao", 25); Hashmap.put ("Sima Yi", 26); Hashmap.put (NULL,NULL); for(Map.entry<string, object>Entry:hashMap.entrySet ()) {System.out.println (Entry.getkey ()+ ": " +Entry.getvalue ()); } } Public Static voidTestlinkedhashmap () {Linkedhashmap<String,Object> Linkedhashmap =NewLinkedhashmap<string, object>(); Linkedhashmap.put ("Zhao Yun", 23); Linkedhashmap.put ("Caocao", 24); Linkedhashmap.put ("Ma Chao", 25); Linkedhashmap.put ("Sima Yi", 26); Linkedhashmap.put (NULL,NULL); Linkedhashmap.put ("2", 1); Linkedhashmap.put ("1", 1); for(Map.entry<string, object>Entry:linkedHashMap.entrySet ()) {System.out.println (Entry.getkey ()+ ": " +Entry.getvalue ()); } } Public Static voidtesthashtable () {Hashtable<String,Object> hashtable =NewHashtable<string, object>(); Hashtable.put ("Zhao Yun", 23); Hashtable.put ("Caocao", 24); Hashtable.put ("Ma Chao", 25); Hashtable.put ("Sima Yi", 26);//Hashtable.put (null,null); for(Map.entry<string, object>Entry:hashtable.entrySet ()) {System.out.println (Entry.getkey ()+ ": " +Entry.getvalue ()); } } Public Static voidMain (string[] args) { System.out.println ( "--hashmap--: Features: unordered, non-thread safe, parent: abstractmap-allow null Key/value"); Mymap.testhashmap (); //Bottom-level implementation: is a hash table (array with single-linked list implementation), with hashcode positioning to deposit a storage space
System.out.println ("--hashtable--Features: unordered, thread safe, parent class dirtory, disallow null Key/value"); Mymap.testhashtable (); //The underlying implementation is consistent with HashMap just adding synchronized to all methods
System.out.println ("-linkhashmap--Features: Orderly!" ( The order here is not meant to be sorted! Instead, it is returned in the order in which it was inserted) non-thread safe, the parent class HashMap allow null Key/value "); Mymap.testlinkedhashmap ();//consistent with the implementation of HashMap, the only difference is the use of a two-way loop linked list, adding before pointers, so you can easily find the previous node to achieve an orderly
System.out.println ("--treemap--Features: Key value ordered non-thread safe parent class: Abstractmap allows value to be null key is forbidden to null"); Mymap.testaddtreemap ();//The bottom-level implementation is a balanced binary tree (red-black tree) to sort the key if you need key to be ordered to use, here TreeMap set a 2 and 1 key results show that the key is sorted }}
HASHMAP underlying data structure display diagram
HashMap is a combination of an array and a single linked list, while the Linkhashmap is roughly the same as the above, but the only difference is that he uses a doubly-linked list so there's a before in addition to next so you can find the previous element in order.
Map of the Java Collection framework