1.

Differentiate between HashSet and HashMap.

Answer»

HashSet is a Set Interface implementation that does not allow duplicate values. The essential point is that objects STORED in HashSet must override equals() and HASHCODE() methods to ensure that no duplicate values are stored in our set.

HASHMAP is a Map Interface implementation that maps a key to a value. In a map, duplicate keys are not permitted.

HashSetHashMap
It implements the Set Interface.It implements the Map Interface.
It does not allow duplicate values.The key needs to be unique while two different keys can have the same value.
While adding an element it requires only one object as a parameter.While adding an entry, it requires two object values, the Key and the Value as the parameter.
Internally, HashSet uses HashMap to add entries. The key K in a HashSet is the argument supplied in the add(Object) method. For each value supplied in the add(Object) method, Java assigns a dummy value.There is no CONCEPT of duplicate values.
It is slower than HashMap.It is faster than HashSet.
It uses the add() method for adding elements.It uses the put() method for adding data elements.


Discussion

No Comment Found