This example shows how to check if an element exists in HashSet in Java. This example also shows how to check if the HashSet contains the element using the contains method.
How to check if element exists in Java HashSet using the contains method?
The contains
method of the HashSet class returns true if the specified object exists in the HashSet.
1 |
public boolean contains(Object o) |
The contains
method returns true if the HashSet contains the specified element, false otherwise.
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 |
import java.util.HashSet; import java.util.Set; public class JavaHashSetContainsExample { public static void main(String[] args) { Set<String> hSetOddNumbers = new HashSet<String>(); hSetOddNumbers.add("One"); hSetOddNumbers.add("Three"); hSetOddNumbers.add("Five"); hSetOddNumbers.add("Seven"); /* * To check if the element exists in the HashSet, use * the contains method */ //this will return true as HashSet contains element "Three" System.out.println( hSetOddNumbers.contains("Three") ); //this will return false as HashSet does not contain element "Four" System.out.println( hSetOddNumbers.contains("Four") ); } } |
Output
1 2 |
true false |
How to check if an object of a custom class exists in the HashSet?
The HashSet contains
method relies on the equals
and hashCode
methods to check if the set contains the specified element. For the HashSet of custom class objects, if the custom class does not override the equals
and hashCode
methods then the contains
method fails to find the specified object.
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 |
import java.util.HashSet; import java.util.Set; class Account{ private int accountNumber; public Account(int accountNumber){ this.accountNumber = accountNumber; } public String toString(){ return "Account: " + this.accountNumber; } } public class JavaHashSetContainsExample { public static void main(String[] args) { Set<Account> hSetAccounts = new HashSet<Account>(); hSetAccounts.add(new Account(100)); hSetAccounts.add(new Account(101)); hSetAccounts.add(new Account(102)); System.out.println( hSetAccounts.contains(new Account(101)) ); } } |
Output
1 |
false |
As we can see from the output, even if the account with id 101 exists in the HashSet object, the contains
method returned false. When the custom class does not override the equals
and hashCode
methods, methods inherited from the Object class are used. The equals
method of the Object class compares the object references not the actual object content and thus returns false.
In order for the contains
method to work properly, we need to implement the equals
and hashCode
methods in our custom class as given below.
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 |
import java.util.HashSet; import java.util.Set; class Account{ private int accountNumber; public Account(int accountNumber){ this.accountNumber = accountNumber; } public String toString(){ return "Account: " + this.accountNumber; } public int hashCode() { final int prime = 31; int result = 1; result = prime * result + accountNumber; return result; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Account other = (Account) obj; if (accountNumber != other.accountNumber) return false; return true; } } public class JavaHashSetContainsExample { public static void main(String[] args) { Set<Account> hSetAccounts = new HashSet<Account>(); hSetAccounts.add(new Account(100)); hSetAccounts.add(new Account(101)); hSetAccounts.add(new Account(102)); System.out.println( hSetAccounts.contains(new Account(101)) ); } } |
Output
1 |
true |
As we can see from the output, this time the contains
method returned true as expected.
This example is a part of the HashSet in Java Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 HashSet