In this blog post we will discuss about one of the very important java interview question What all collections utilizes hashCode() method in java?
- Only hashing data structures uses hashCode() method along with equals() method, though the equals() is used by almost every class.
- hashCode is useful for creating hashing based datastructures like HashMap, LinkedHashMap, ConcurrentHashMap, HashSet. (Basically any Java collection that has Hash inside the name of it)
- Hashcode is used to provide best case O(1) time complexity for searching the stored element.
- TreeMap, TreeSet uses Comparator/Comparable for comparing the elements against each other, so these data structures do not require hashCode() method. The best-case time complexity offered by these data structures for lookup operation is logarithmic rather than constant.
You may also like:
When should we choose LinkedList over ArrayList for a given Scenario and Why?
Is there concurrent version for TreeMap and TreeSet in Java Collections Framework?
What are fail-fast and fail-safe Iterator?
What is difference between Iterator and LisIterator?
Why Prime Numbers are considered in writing certain algorithms like hashcode()?
That’s all about What all collections utilizes hashCode() method in java?
If you have any feedback or suggestion please feel free to drop in blow comment box.