How to use the functional interface built into java
This article mainly introduces the java built-in functional interface how to use the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that after reading this java built-in functional interface how to use the article will have a harvest, let's take a look.
1. Predicate is a Boolean function of a parameter. This interface provides many default functions to combine complex logical operations (and, non).
Predicate predicate = (s)-> s.length () > 0; predicate.test ("foo"); / / truepredicate.negate () .test ("foo"); / / false Predicate nonNull = Objects::nonNull;Predicate isNull = Objects::isNull; Predicate isEmpty = String::isEmpty;Predicate isNotEmpty = isEmpty.negate ()
2. Function receives parameters to produce results. The default method can be used in a method chain consisting of a variety of methods.
Function toInteger = Integer::valueOf;Function backToString = toInteger.andThen (String::valueOf); backToString.apply ("123"); / /" 123"
3. Supplier generates objects according to the given class attributes, and Supplier does not support input parameters.
Supplier personSupplier = Person::new;personSupplier.get (); / / new Person's article on "how to use java's built-in functional interface" ends here. Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to use the built-in functional interface of java". If you want to learn more, you are welcome to follow the industry information channel.