Junior — Middle
Which interface implements the filter method in functional programming?
sobes.tech AI
Answer from AI
In Java, the filter method is used in the Stream API and takes a functional interface Predicate<T> as a parameter. This interface represents a function that accepts an object of type T and returns a boolean value (true or false). The filter method only passes through those stream elements for which the Predicate returns true.
Example:
List<String> names = List.of("Anna", "Bob", "Alice", "Tom");
List<String> filtered = names.stream()
.filter(name -> name.startsWith("A")) // Predicate<String>
.collect(Collectors.toList());
System.out.println(filtered); // [Anna, Alice]