Use equal objects as a key in HashMap or HashSet in Java
This post will discuss how to use equal objects as a key in HashMap or HashSet in Java by overriding the equals() and hashCode() method of the object.
Problem:
If a class does not override the equals() and hashCode() methods of the Object class and an object of such class is used as a key for map or set in Java, the default implementation of these methods are used which simply check for reference equality.
Let’s consider the following example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
import java.util.HashSet; import java.util.Objects; import java.util.Set; class Person { private String name; private int age; // Constructor Person(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "{" + name + ", " + age + "}"; } } class Main { public static void main(String[] args) { Person p1 = new Person("John", 20); Person p2 = new Person("John", 20); Person p3 = new Person("Carol", 16); Set<Person> set = new HashSet<>(); set.add(p1); set.add(p2); set.add(p3); System.out.println(set); } } |
Output:
[{John, 20}, {John, 20}, {Carol, 16}]
In the above example, p1 and p2 are considered two separate keys by the HashSet even though they have equal fields. Therefore, two keys will only be equal if they are, in fact, the same object.
Solution:
If we need key equality on different objects with the same value of instance variables, we need to implement equals() and hashCode() on the key.
There are several methods to override equals() and hashCode() methods in Java. The following example uses static utility methods introduced with the Objects class in Java 7 that provides for computing the hash code of an object and comparing two objects.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
import java.util.HashSet; import java.util.Objects; import java.util.Set; class Person { private String name; private int age; // Constructor Person(String name, int age) { this.name = name; this.age = age; } @Override public boolean equals(Object ob) { if (ob == this) { return true; } if (ob == null || ob.getClass() != getClass()) { return false; } Person p = (Person) ob; return Objects.equals(name, p.name) && p.age == age; } @Override public int hashCode() { return Objects.hash(name, age); } @Override public String toString() { return "{" + name + ", " + age + "}"; } } class Main { public static void main(String[] args) { Person p1 = new Person("John", 20); Person p2 = new Person("John", 20); Person p3 = new Person("Carol", 16); Set<Person> set = new HashSet<>(); set.add(p1); set.add(p2); set.add(p3); System.out.println(set); } } |
Output:
[{Carol, 16}, {John, 20}]
That’s all about using equal objects as a key in HashMap or HashSet in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)