Home > Java > javaTutorial > body text

How to use java's built-in functional interface

PHPz
Release: 2023-05-02 18:28:07
forward
1016 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 How to use java's built-in functional interface. 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