The difference between HashMap, LinkedHashMap and TreeMap

This translation from: Difference the BETWEEN HashMap, TreeMap and LinkedHashMap

IS at The -difference the BETWEEN the What HashMap, LinkedHashMapand TreeMapin the Java? The Java in HashMap, LinkedHashMapand TreeMapwhat is the difference? See the any -difference not do the I in the Output at The AS All at The Three has keySetand values. I do not see any difference in output, because all three have keySetand values. It is the What HashtableS? What is Hashtable?

Map m1 = new HashMap();
m1.put("map", "HashMap");
m1.put("schildt", "java2");
m1.put("mathew", "Hyden");
m1.put("schildt", "java2s");
print(m1.keySet()); 
print(m1.values()); 

SortedMap sm = new TreeMap();
sm.put("map", "TreeMap");
sm.put("schildt", "java2");
sm.put("mathew", "Hyden");
sm.put("schildt", "java2s");
print(sm.keySet()); 
print(sm.values());

LinkedHashMap lm = new LinkedHashMap();
lm.put("map", "LinkedHashMap");
lm.put("schildt", "java2");
lm.put("mathew", "Hyden");
lm.put("schildt", "java2s");
print(lm.keySet()); 
print(lm.values());

#1st Floor

Reference: The difference between the https://stackoom.com/question/C7lJ/HashMap-LinkedHashMap and TreeMap


#2nd Floor

Just some more input from my own experience with maps, on when I would use each one: get more input from the map of my own experience, when I use each map:

  • HashMap - Most Useful looking for the when-A Best Performance (the FAST) Implementation. HashMap - most useful when optimal performance (fast) are looking to achieve.
  • TreeMap (SortedMap interface) - Most Useful Concerned with the when the I'm being or the Sort of Able to iterate over at The Keys in the Order that the I the DEFINE A Particular. TreeMap (SortedMap Interface) - When I was able to care for my keys in a specific order defined when ordering or iteration, the most useful.
  • A LinkedHashMap - Combines Advantages of Guaranteed Ordering from the without TreeMap The Increased cost of Maintaining The TreeMap. A LinkedHashMap - the cost of maintaining the advantages of binding TreeMap TreeMap guaranteed ordering, without increasing. (It IS AS the FAST AS Almost at The HashMap). (It is almost as fast as HashMap). Particular an In, at The LinkedHashMap Also the Provides A Great Starting Point for Creating A Cache Object by overriding at The removeEldestEntry()Method,. In particular, LinkedHashMap also by covering removeEldestEntry()provides a good starting point method to create the Cache object. This lets you create a Cache object that can expire data using some criteria that you define. This allows you to create the Cache object can make use of the data due to certain conditions you define.

#3rd floor

Makes the About Guarantees. Absolutely not HashMap at The the Iteration the Order. HashMap absolutely no guarantee that the iteration order. It can (and will) even change completely when new elements are added. When adding a new element, it can even (and) completely changed. The TreeMap Will the iterate ACCORDING to The "Natural Ordering" of The Keys ACCORDING to Their compareTo () Method (or AN externally Supplied Comparator). The TreeMap the () method which compareTo (or externally provided Comparator) The "natural ordering" key iterate. Additionally, it implements the SortedMap interface, which contains methods that depend on this sort order. In addition, it implements SortedMap interface, which method comprises rely on this sort order. LinkedHashMap will iterate in the order in which the entries were put into the map iterate LinkedHashMap will follow the order of entry into the map

Look at how performance varying .. to see how performance changes .. The input image description

Tree map which is an implementation of Sorted map. Tree ordering is an implementation of FIG. The complexity of the put, get and containsKey operation is O (log n) due to the Natural ordering due to the natural order, complexity put, get and containsKey operation is O (log n)


#4th floor

I prefer visual presentation: I prefer the visual presentation:

╔══════════════╦═════════════════════╦═══════════════════╦═════════════════════╗
║   Property   ║       HashMap       ║      TreeMap      ║     LinkedHashMap   ║
╠══════════════╬═════════════════════╬═══════════════════╬═════════════════════╣
║ Iteration    ║  no guarantee order ║ sorted according  ║                     ║
║   Order      ║ will remain constant║ to the natural    ║    insertion-order  ║
║              ║      over time      ║    ordering       ║                     ║
╠══════════════╬═════════════════════╬═══════════════════╬═════════════════════╣
║  Get/put     ║                     ║                   ║                     ║
║   remove     ║         O(1)        ║      O(log(n))    ║         O(1)        ║
║ containsKey  ║                     ║                   ║                     ║
╠══════════════╬═════════════════════╬═══════════════════╬═════════════════════╣
║              ║                     ║   NavigableMap    ║                     ║
║  Interfaces  ║         Map         ║       Map         ║         Map         ║
║              ║                     ║    SortedMap      ║                     ║
╠══════════════╬═════════════════════╬═══════════════════╬═════════════════════╣
║              ║                     ║                   ║                     ║
║     Null     ║       allowed       ║    only values    ║       allowed       ║
║ values/keys  ║                     ║                   ║                     ║
╠══════════════╬═════════════════════╩═══════════════════╩═════════════════════╣
║              ║   Fail-fast behavior of an iterator cannot be guaranteed      ║
║   Fail-fast  ║ impossible to make any hard guarantees in the presence of     ║
║   behavior   ║           unsynchronized concurrent modification              ║
╠══════════════╬═════════════════════╦═══════════════════╦═════════════════════╣
║              ║                     ║                   ║                     ║
║Implementation║      buckets        ║   Red-Black Tree  ║    double-linked    ║
║              ║                     ║                   ║       buckets       ║
╠══════════════╬═════════════════════╩═══════════════════╩═════════════════════╣
║      Is      ║                                                               ║
║ synchronized ║              implementation is not synchronized               ║
╚══════════════╩═══════════════════════════════════════════════════════════════╝

#5th Floor

WHERE IS in each class See The class in Hierarchy Diagram The following ( Bigger One .) Below the position of FIG view each class (the class hierarchy in the larger one ). The implements TreeMap SortedMapand NavigableMapthe while HashMapdoes not. TreeMap implements SortedMapand NavigableMapwhich HashMapdo not.

HashTableThe Obsolete and the Corresponding IS ConcurrentHashMapclass Should BE Used. HashTableobsolete, should use the corresponding ConcurrentHashMapclass. The input image description


#6th floor

These are different implementations of the same interface . These are the different implementations of the same interface. Each implementation has some advantages and some disadvantages (fast insert, slow search) or vice versa. Each implementation has some advantages and some disadvantages (rapid insertion, slow search), and vice versa.

AT the Details look at The javadoc the For of TreeMap , HashMap , LinkedHashMap . For more information, please see the TreeMap , HashMap , LinkedHashMap of javadoc.

Original articles published 0 · won praise 73 · views 550 000 +

Guess you like

Origin blog.csdn.net/w36680130/article/details/105330281