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.
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)
perfect. first 30 seconds answered my question :) thank you! :D
God bless u man, thank you for your selflessly sharing of knowledge
Nice Video! Thx
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.
Thanks for sharing. But how LinkedHashMap maintains Insertion order is not explained in detail. Head and Tail variables are not mentioned.
Thank you so much for sharing
Why is it called 'linked' ?