Introduction In custom object as a key in hashmap example, I will show you how to work with user defined objects as keys in Map. To use user defined objects as keys in Map you need to tell Java how to compare user defined objects by overriding the hashCode() and equals() methods. Most of the […]
What is HashSet HashSet extends AbstractSet and is an implementation of Set interface. HashSet also implements Serializable and Cloneable interfaces. HashSet is backed by hash table(actually HashMap instance), i.e., the HashSet uses hash table(HashMap) to store collection elements. Like HashMap, it allows null only one element.
In Java, every object has access to the equals() method because it is inherited from the Object class. However, this default implementation just simply compares the memory addresses of the objects. You can override the default implementation of the equals() method defined in java.lang.Object. If you override the equals(), you must override hashCode() otherwise a […]