Custom Object as a Key in HashMap

Introduction In this Java HashMap example I am going to tell you how to use custom object as a key in HashMap. 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 time you generally use Java API provided classes, for example, mostly String objects as…

Continue

Override equals() and hashCode() method in Java

Introduction Here we will discuss how to override equals() and hashCode() method in Java. In Java language the important contract is whenever you override one the methods (equals() and hashCode()), then you must override the other method. Features of equals() and hashCode() 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 class. If you override…

Continue