This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details
“The difference between HashMap, LinkedHashMap and TreeMap?
What is the difference between HashMap, LinkedHashMap and TreeMap in Java? I don’t see any difference in the output because all three have keySet and values. What is Hashtables?
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());
Copy the code
Answer:
All three classes implement the Map interface and provide much the same functionality. The most important difference is the order in which you iterate through items:
A HashMap absolutely does not guarantee iteration order. It can (and will) even change completely when new elements are added. TreeMap iterates based on the “natural order” of the key (according to its compareTo() method) (or externally provided comparators). In addition, it implements the SortedMap interface, which contains methods that depend on this sort order. The LinkedHashMap will iterate in the order in which the entries are put into the map
“Hash table” is a generic name for a mapping based on hash. In the context of the Java API, Hashtable is an obsolete class from Java 1.1 to the pre-existence of the Collection framework. Don’t use it anymore because its API is full of outdated methods that duplicate functionality, and its methods are synchronous (which can degrade performance and is often useless). Use ConcurrentHashMap instead of Hashtable.
Answer:
These three represent mappings from unique keys to values, thus implementing the Map interface.
A HashMap is a mapping based on key hashing. It supports O (1) get/PUT operations. The key must have a consistent implementation, hashCode(), and equals() to work properly.
LinkedHashMap is very similar to HashMap, but it adds awareness of the order in which items are added (or accessed), so that the iteration order is the same as the insertion order (or access order, depending on the construction parameters).
TreeMap is a tree-based mapping. Its place/fetch operation takes O (log n) time. It requires that items have some kind of comparison mechanism that can be compared or compared. The iteration order is determined by this mechanism.
The article translated from yl2gl72eozkinivz3vc6swkesy – ac4c6men2g7xr2a – translate. Translate. Goog/questions / 2…
The authors suggest:
Hashmap contains a lot of topics, such as jdk1.7’s head insertion loop problem, 1.8’s tail insertion problem, the initial capacity of hashmap, expansion, hash algorithm, thread safety (concurrenthashmap-cas +sync), and the calculation of size.
JDK1.8 size is obtained by CAS calculation of baseCount and counterCell, and finally by baseCount and counterCell traversal. The main idea is to improve throughput through arrays.
It is recommended to read the source code several times to deepen the impression.
Thank you for reading this, if this article is well written and if you feel there is something to it
Ask for a thumbs up 👍 ask for attention ❤️ ask for share 👥 for 8 abs I really very useful!!
If there are any mistakes in this blog, please comment, thank you very much! ❤ ️ ❤ ️ ❤ ️ ❤ ️