What is LinkedHashMap in Java? - Explained | Java collections #10 | java9s

preview_player
Показать описание
LinkedHashMap is a data structure Which maintains the insertion order for its entries. That means it arranges its entries exactly in the same order as they have been added. LinkedHashMap can be used in the scenarios where the First in first out kind of data model is needed.

LinkedHashMap can also maintain the access order for its entries where it moves all recently accessed entries to the bottom of the collection.

This kind of feature is useful in making LRU (Least Recently Used) caching mechanisms.

Complete Generics Tutorials playlist

Complete Java Beginners Tutorials:

Multithreading in Java tutorial:

Spring 3 MVC Framework tutorials:

Spring Framework Tutorials:

Design Patterns in Java:

Java Collections Tutorials:
Рекомендации по теме
Комментарии
Автор

Thumbs up for LRU concept you explained at the end, it makes so much sense when you relate a Collection type to real time usecase. Good stuff (Y)

suman
Автор

So .equals() and .hashCode() are absolutely essential for hashSet and hashMap keys, because first .hashCode() and then .equals() in case of ties are used to identify elements/entries in these. It is nice to have that .equals() be consistent with the .compareTo() or .compare() method that is used to sort the elements or keys in a TreeSet or TreeMap, but again, TreeSet and TreeMap do use the .compareTo() or .compare() methods of the class itself or the custom Comparator object passed in to sort. I feel some people watching this video may have been confused about this. So, with the three basic main kinds of set/map, the ordering is based on:
kind ordered/matched by

Hash --- unordered, matched up by .hashCode() and .equals()
LinkedHash --- ordered by original insertion order, but looked up by .hashCode()/.equals() as above
Tree --- sorted by natural sort ordering .compareTo() or custom .compare() of Comparator

And reminding us one more time, HashSet is not sorted, LinkedHashSet is not sorted, TreeSet is both SortedSet and NavigableSet. The Sets are actually based on the Maps, just without values. LinkedHashMap can give back the keys in order of most recently referenced instead of original insertion order, but requires a special parameter in the constructor to opt in to this behavior.

jvsnyc
Автор

perfect. first 30 seconds answered my question :) thank you! :D

lytwyna
Автор

God bless u man, thank you for your selflessly sharing of knowledge

mils
Автор

Thanks for sharing. But how LinkedHashMap maintains Insertion order is not explained in detail. Head and Tail variables are not mentioned.

rajki