1. Predicate is a Boolean function of parameters. The interface provides a number of default functions to combine complex logical operations (AND, NOT).
Predicate<String> predicate = (s) -> s.length() > 0; predicate.test("foo"); // true predicate.negate().test("foo"); // false Predicate<Boolean> nonNull = Objects::nonNull; Predicate<Boolean> isNull = Objects::isNull; Predicate<String> isEmpty = String::isEmpty; Predicate<String> isNotEmpty = isEmpty.negate();
2. Function receives parameters to produce results. Default methods can be used in method chains consisting of multiple methods.
Function<String, Integer> toInteger = Integer::valueOf; Function<String, String> backToString = toInteger.andThen(String::valueOf); backToString.apply("123"); // "123"
3. Supplier generates objects based on given class attributes. Supplier does not support input parameters.
Supplier<Person> personSupplier = Person::new; personSupplier.get(); // new Person
The above is the detailed content of How to use java's built-in functional interface. For more information, please follow other related articles on the PHP Chinese website!