Junior — Middle
What is the name of the interface used to pass filtering conditions in a functional style?
sobes.tech AI
Answer from AI
In Java, the Predicate<T> interface is used to pass filtering conditions in a functional style. It represents a function that takes an object of type T and returns a boolean value (true or false), indicating whether the object satisfies the filtering condition.
Example of usage with list filtering:
List<String> names = Arrays.asList("Anna", "Bob", "Charlie");
List<String> filtered = names.stream()
.filter(name -> name.startsWith("A")) // here, Predicate<String> is used
.collect(Collectors.toList());
System.out.println(filtered); // Outputs: [Anna]
The Predicate interface is often used in the Stream API to implement collection filtering.