Java does not have a direct method to find elements / objects in a HashSet. To do so, you can use Java 8+ Stream API, Java 8+ Iterable's forEach(Consumer), or looping though iterator

Apart from that, you can also check if one set contains a specified element or collection with contains(Object) and containsAll(Collection) methods

Let's walk through the following examples to find more details

Find elements / objects

  • The Java 8+ filter(Predicate) method of Stream API returns a stream of elements matching with the specified predicate
@Test
public void findWithStreamAPI() {  
    Set<Integer> set = new HashSet<>();
    set.add(3);
    set.add(1);
    set.add(2);

    Integer obj = set.stream()
        .filter(ele -> ele.equals(1))
        .findFirst()
        .orElse(null);
    assertThat(obj).isEqualTo(1);
}
  • With Java 8+ forEach(Consumer)
@Test
public void findWithForEachConsumer() {  
    Set<Integer> set = new HashSet<>();
    set.add(3);
    set.add(1);
    set.add(2);

    set.forEach(ele -> {
        if (ele.equals(1)) {
            System.out.println(ele);
        }
    });
}
  • With for-each loop
@Test
public void findWithForEach() {  
    Set<Integer> set = new HashSet<>();
    set.add(3);
    set.add(1);
    set.add(2);

    for (Integer ele : set) {
        if (ele.equals(1)) {
            System.out.println(ele);
        }
    }
}
  • Loop through iterator to find elements
@Test
public void findWithIterator() {  
    Set<Integer> set = new HashSet<>();
    set.add(3);
    set.add(1);
    set.add(2);

    Iterator<Integer> iterator = set.iterator();
    while (iterator.hasNext()) {
        Integer obj = iterator.next();
        if (obj.equals(1)) {
            System.out.println(obj);
        }
    }
}

Check if existing elements / objects

  • The contains(Object) returns true if containing the specified element, false otherwise
@Test
public void contains() {  
    Set<Integer> set = new HashSet<>();
    set.add(3);
    set.add(1);
    set.add(2);

    boolean result = set.contains(1);
    assertThat(result).isTrue();

    result = set.contains(4);
    assertThat(result).isFalse();
}
  • The containsAll(Collection) returns true if containing all elements in the specified collection, false otherwise
@Test
public void containsAll() {  
    Set<Integer> set = new HashSet<>();
    set.add(3);
    set.add(1);
    set.add(2);

    boolean result = set.containsAll(Set.of(1, 2));
    assertThat(result).isTrue();

    result = set.containsAll(Set.of(1, 4));
    assertThat(result).isFalse();
}

Size of a HashSet

  • size() returns the number of elements in a HashSet
@Test
public void size() {  
    Set<Integer> set = new HashSet<>();
    set.add(3);
    set.add(1);
    set.add(2);

    int size = set.size();
    assertThat(size).isEqualTo(3);
}
  • isEmpty() returns true if there're no elements in a HashSet, false otherwise
@Test
public void isEmpty() {  
    Set<Integer> set = new HashSet<>();
    set.add(3);
    set.add(1);
    set.add(2);

    boolean result = set.isEmpty();
    assertThat(result).isFalse();
}


Conclusion

In this tutorial, we learned some query operations on a HashSet in Java. You can find the full source code as below