Home >Java >javaTutorial >What built-in functional interfaces does Java have?

What built-in functional interfaces does Java have?

PHPz
PHPzforward
2023-05-09 15:52:07924browse

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 What built-in functional interfaces does Java have?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete