HashSet is one of the most frequently asked interview question during any level of core java interview. Usually, Collection framework and multi-threading interview questions are mostly on demand for core java interview question.
HashSet are integral part of Collection framework in java. These are most frequently used collection classes, it means ,
List in java.
Let us go through one by one both of these i.e.
HashSet and afterward will go for Differences between them i.e.
HashMap in Java?
HashMap is an implementation of
Interface, store key and value , it means, maps a key to its value. Basically, map Interface has three implementation classes
TreeMap. It works on hasing technique, so complexty of getting element is O(1).
Key Points of HashMap are:
- Duplicate keys are not allowed in a map i.e. HashMap but duplicate values are allowed.
- HashMap not maintain insertion order.
- HashMap allows null values and one null keys.
- HashMap is not synchronized, but collection framework provide way to make them synchronized using
- public Object put(Object Key,Object value) method is used to add an element in the map.
HashSet in Java?
HashSet is implementation of Set Interface which does not allow duplicate value. basic fundamental is Set does not allow null, duplicate element and not maintain insertion order , maintain sorting order.
Key Points about HashSet are:
- Does not allow null
- it maintain sorting order not insertion order
- objects which are going to be stored in HashSet must override equals() and hashCode() method, so that , can check for equality and no duplicate value will stored in set.
- add(Object o) returns true if unique value added , if duplicate then returns false.
Difference between HashSet and HashMap in Java
differences between HashMap and HashSet are as::[table “” not found /]
Please let me know by writing comment on this post, if you want to add any other difference between HashSet and HashMap in Java or any suggestions.