Home > Java > javaTutorial > body text

What built-in functional interfaces does Java have?

PHPz
Release: 2023-05-09 15:52:07
forward
888 people have browsed it

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();
Copy after login

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"
Copy after login

3. Supplier generates objects based on given class attributes. Supplier does not support input parameters.

Supplier<Person> personSupplier = Person::new;
personSupplier.get();   // new Person
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template