Home  >  Article  >  Java  >  How to use java's built-in functional interface

How to use java's built-in functional interface

WBOY
WBOYforward
2023-05-04 15:22:061158browse

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();

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"

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

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!

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