This example shows how to check if an element exists in LinkedHashSet in Java. This example also shows how to check if LinkedHashSet contains the specified element using the contains method.
How to check if an element exists in LinkedHashSet in Java?
The contains
method of the LinkedHashSet class returns true if the specified element is found in the LinkedHashSet object.
1 |
public boolean contains(Object object) |
The contains
method returns true if this set object 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 |
import java.util.LinkedHashSet; public class LinkedHashSetContainsExample { public static void main(String[] args) { LinkedHashSet<String> lhSetColors = new LinkedHashSet<String>(); lhSetColors.add("red"); lhSetColors.add("green"); lhSetColors.add("blue"); /* * To check if the LinkedHashSet contains the element, * use the contains method. */ //this will return true as the "red" element exists System.out.println( lhSetColors.contains("red") ); //this will return true as the "white" element does not exists System.out.println(lhSetColors.contains("white")); } } |
Output
1 2 |
true false |
How to check if LinkedHashSet contains an object of a custom class?
The LinkedHashSet contains
method relies on the equals
method of the elements to compare and find the objects.
If the LinkedHashSet elements are objects of a custom class, then the custom class must implement the equals
and hashCode
methods for the contains
method to work properly.
Let’s first see an example of that.
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 |
import java.util.LinkedHashSet; class Subject{ int subjectId; public Subject(int subjectId){ this.subjectId = subjectId; } public int getSubjectId(){ return this.subjectId; } public String toString(){ return "Subject -> " + getSubjectId(); } } public class LinkedHashSetContainsExample { public static void main(String[] args) { LinkedHashSet<Subject> lhSetSubjects = new LinkedHashSet<Subject>(); lhSetSubjects.add( new Subject(1) ); lhSetSubjects.add( new Subject(2) ); lhSetSubjects.add( new Subject(3) ); System.out.println( lhSetSubjects.contains(new Subject(2)) ); } } |
Output
1 |
false |
As we can see from the output, even if the specified custom class object was present in the LinkedHashSet, the contains
method returned false.
In order to solve the problem of contains
method returning false for the custom class objects, we need to override the equals
and hashCode
methods in the Subject 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 50 51 52 |
import java.util.LinkedHashSet; class Subject{ int subjectId; public Subject(int subjectId){ this.subjectId = subjectId; } public int getSubjectId(){ return this.subjectId; } public String toString(){ return "Subject -> " + getSubjectId(); } public int hashCode() { final int prime = 31; int result = 1; result = prime * result + subjectId; return result; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Subject other = (Subject) obj; if (subjectId != other.subjectId) return false; return true; } } public class LinkedHashSetContainsExample { public static void main(String[] args) { LinkedHashSet<Subject> lhSetSubjects = new LinkedHashSet<Subject>(); lhSetSubjects.add( new Subject(1) ); lhSetSubjects.add( new Subject(2) ); lhSetSubjects.add( new Subject(3) ); System.out.println( lhSetSubjects.contains(new Subject(2)) ); } } |
Output
1 |
true |
As we can see from the output, the LinkedHashSet contains
method worked as expected this time.
This example is a part of the LinkedHashSet in Java Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 LinkedHashSet