Sobes.tech
Junior — Middle

What does the filter method look like in the Stream interface in Java?

sobes.tech AI

Answer from AI

In the Stream interface in Java, the filter method is declared approximately as follows:

Stream<T> filter(Predicate<? super T> predicate);

Where:

  • T is the type of stream elements.
  • Predicate<? super T> is a functional interface that takes an element and returns a boolean, determining whether to pass the element further.

Example of usage:

Stream<String> stream = Stream.of("apple", "banana", "cherry");
Stream<String> filtered = stream.filter(s -> s.startsWith("b"));
filtered.forEach(System.out::println); // Will output "banana"
What does the filter method look like in the Stream… - sobes.tech