Junior — Middle
Kuidas nimetatakse liidest, mida kasutatakse filtritingimuste edastamiseks funktsionaalses stiilis?
sobes.tech AI
Vastus AI-lt
In Java, to pass filtering conditions in a functional style, the Predicate<T> interface is used. 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.