Home > Java > javaTutorial > body text

How to use java built-in functional interface

WBOY
Release: 2023-05-21 10:28:05
forward
831 people have browsed it

1. Predicate is a Boolean function with only one input parameter. The Predicate interface contains a variety of default methods to handle complex logical verbs.

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. The Function interface receives a parameter and returns a single result. By default, multiple functions can be chained together.

Function<String, Integer> toInteger = Integer::valueOf;
Function<String, String> backToString = toInteger.andThen(String::valueOf);
 
backToString.apply("123");     // "123"
Copy after login

3. The Supplier interface produces results of a given type. Unlike Function, Supplier has no 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 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