Check if an element is present in a Set in Java
This post will discuss how to check if an element is present in a Set in Java.
1. Using Set.contains() method
The standard solution to check if an element is present in a Set is using the contains(o) method. It returns true if the set contains an element e such that Objects.equals(o, e) holds.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.Set; public class Main { public static void main(String[] args) { Set<Integer> values = Set.of(4, 2, 3, 1, 5); int item = 3; boolean contains = values.contains(item); System.out.println(contains); // true } } |
Note that if a class does not override the equals() and hashCode() methods, the default implementation of these methods only checks for the reference equality. In order words, if an object of such class is inserted in a Set, the contains() method will return false. For instance, consider the following code which returns false:
|
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 |
import java.util.Set; class Point { private int x, y; private Point(int x, int y) { this.x = x; this.y = y; } public static Point of(int x, int y) { return new Point(x, y); } } public class Main { public static void main(String[] args) { Set<Point> cells = Set.of(Point.of(0, 0), Point.of(1, 2), Point.of(3, 4), Point.of(5, 6)); Point origin = Point.of(0, 0); boolean contains = cells.contains(origin); System.out.println(contains); // false } } |
To fix this, simply override equals and hashCode methods. Both these methods can be auto-generated by IDE (Eclipse, IntelliJ IDEA, etc.).
|
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 |
import java.util.Objects; import java.util.Set; class Point { private int x, y; private Point(int x, int y) { this.x = x; this.y = y; } public static Point of(int x, int y) { return new Point(x, y); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Point point = (Point) o; return x == point.x && y == point.y; } @Override public int hashCode() { return Objects.hash(x, y); } } public class Main { public static void main(String[] args) { Set<Point> cells = Set.of(Point.of(0, 0), Point.of(1, 2), Point.of(3, 4), Point.of(5, 6)); Point origin = Point.of(0, 0); boolean contains = cells.contains(origin); System.out.println(contains); // true } } |
2. Using Stream.anyMatch() method
In Java 8 and above, you can use the Stream.anyMatch() method that returns true if any element of the stream matches with the specified predicate.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.Set; public class Main { public static void main(String[] args) { Set<Integer> values = Set.of(4, 2, 3, 1, 5); int item = 3; boolean contains = values.stream().anyMatch(i -> i == item); System.out.println(contains); // true } } |
3. Using Apache Commons Collections
If your project uses the Apache Commons Collections library, you may use the CollectionUtils.containsAny() method, which returns true if any element of the collection matches with any of the specified elements.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import org.apache.commons.collections4.CollectionUtils; import java.util.Set; public class Main { public static void main(String[] args) { Set<Integer> values = Set.of(4, 2, 3, 1, 5); int item = 3; boolean contains = CollectionUtils.containsAny(values, item); System.out.println(contains); // true } } |
4. Using Collections.disjoint() method
Finally, you can use the Collections.disjoint() method in Java, which returns true if the two specified collections have no elements in common. However, this method may suffer from the overhead caused by creating a collection containing the value to be searched.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Collections; import java.util.Set; public class Main { public static void main(String[] args) { Set<Integer> values = Set.of(4, 2, 3, 1, 5); int item = 3; boolean contains = !Collections.disjoint(values, Collections.singleton(item)); System.out.println(contains); // true } } |
That’s all about checking if an element is present in a Set 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 :)