Java Stream contains, containsAny and containsAll Examples

The Java Stream API comes with a rich set of intermediate and final operations, but Stream API doesn’t have built-in contains(), containsAll() or containsAny() methods. In this tutorial, we will write easy-to-follow examples to create these utility methods and learn how to use them.

1. The Utility Methods: contains(), containsAny(), containsAll()

Let us quickly see the list of methods that we are planning to add in a utility class that can be used for checking the contains/containsAny/containsAll functionality.

  • The contains method returns true if the specified element is present in the specified stream, else false.
  • The containsAny method checks the presence of a specified element(s) in the specified stream. Returns true if even a single element is found, else false.
  • The containsAll method checks the presence of a specified element(s) in the specified stream. Returns true if all the elements are found, else false.
public class StreamUtils {

  public static <T> boolean contains(Stream<T> stream, T element){...}

  public static <T> boolean containsAny(Stream<T> stream, Collection<? extends T> elements){...}
  public static <T> boolean containsAny(Stream<T> stream, T... elements){...}
  public static <T> boolean containsAny(Stream<T> stream, Stream<? extends T> elements){...}

  public static <T> boolean containsAll(Stream<T> stream, Collection<? extends T> elements){...}
  public static <T> boolean containsAll(Stream<T> stream, T... elements){...}
  public static <T> boolean containsAll(Stream<T> stream, Stream<? extends T> elements){...}
}

2. Method Implementations

The contains(any/all) methods make use of the Stream.anyMatch() and Stream.allMatch() methods for checking the presence of argument elements. We add the additional checks for null values.

  • The anyMatch() method checks if the Stream contains at least one element that satisfies the given Predicate. This is a terminal short-circuit operation.
  • The allMatch() method checks if all the elements in the stream satisfy the provided Predicate. This is a terminal short-circuit operation.

Please note that if we pass the empty elements list to check, the result will be true.

public final class StreamUtils {

  private StreamUtils() {
    // Private constructor to prevent instantiation
  }

  public static <T> boolean contains(Stream<T> stream, T element) {
    Objects.requireNonNull(stream);
    Objects.requireNonNull(element);
    return stream.anyMatch(Predicate.isEqual(element));
  }

  public static <T> boolean containsAny(Stream<T> stream, Collection<? extends T> elements) {
    return containsAny(stream, elements.stream());
  }

  public static <T> boolean containsAny(Stream<T> stream, T... elements) {
    return containsAny(stream, Stream.of(elements));
  }

  public static <T> boolean containsAny(Stream<T> stream, Stream<? extends T> elements) {
    Objects.requireNonNull(stream);
    Objects.requireNonNull(elements);
    return elements.anyMatch(stream.collect(Collectors.toSet())::contains);
  }

  public static <T> boolean containsAll(Stream<T> stream, Collection<? extends T> elements) {
    return containsAll(stream, elements.stream());
  }

  public static <T> boolean containsAll(Stream<T> stream, T... elements) {
    return containsAll(stream, Stream.of(elements));
  }

  public static <T> boolean containsAll(Stream<T> stream, Stream<? extends T> elements) {
    Objects.requireNonNull(stream);
    Objects.requireNonNull(elements);
    return elements.allMatch(stream.collect(Collectors.toSet())::contains);
  }
}

3. Demo

Let us test the above methods with some dummy data. In this demo, we have a list of cars. The car class is:

@Data
@AllArgsConstructor
@NoArgsConstructor
class Car {

  String model;
  String fuel;
  Integer capacity;
}

For the demo, we have added the following cars in a stream. Also, we have created a few cars that we need to check. Note that we know that only ‘car1‘ is present in the stream so we can verify our assumptions and be certain about the method implementations.

List<Car> cars = Arrays.asList(
  new Car("Dacia", "diesel", 100),
  new Car("Lexus", "gasoline", 300),
  new Car("Ford", "electric", 200)
);

Car car1 = new Car("Dacia", "diesel", 100);  //Present in original list
Car car2 = new Car("Ford", "electric", 80);
Car car3 = new Car("Chevrolet", "electric", 150);

Let’s start with testing the contains() method.

Assertions.assertTrue( StreamUtils.contains(cars.stream(), car1) );
Assertions.assertFalse( StreamUtils.contains(cars.stream(), car2) );

Next, test the containsAny() method. We have passed the arguments in all possible ways i.e. varargs, collection and even another stream.

Assertions.assertTrue( StreamUtils.containsAny(cars.stream(), car1, car2) );
Assertions.assertFalse( StreamUtils.containsAny(cars.stream(), car2, car3) );

Assertions.assertTrue( StreamUtils.containsAny(cars.stream(), List.of(car1, car2, car3)) );  // collection
Assertions.assertTrue( StreamUtils.containsAny(cars.stream(), Stream.of(car1, car2, car3)) );  // stream

Similarly, test the containsAll() method approach. Again we are testing with all sorts of parameter types.

Assertions.assertTrue( StreamUtils.containsAll(cars.stream(), car1) );
Assertions.assertFalse( StreamUtils.containsAll(cars.stream(), car1, car3) );

Assertions.assertTrue( StreamUtils.containsAll(cars.stream(), List.of(car1)) );
Assertions.assertFalse( StreamUtils.containsAll(cars.stream(), Stream.of(car2)) );

4. Conclusion

In this short Java tutorial, we created a Java Stream utility class and added the contains, containsAny and containsAll methods. We can use these methods in our application code directly as these have been declared static methods.

We can also refer to the method implementations if we want to devise our own solutions.

Happy Learning !!

Source Code on Github

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.