Java 8 new Features-Rookie tutorial (3)-java 8 function interface

Source: Internet
Author: User

Java 8 Functional Interface

A functional interface (functional Interface) is an interface that has and has only one abstract method, but can have multiple non-abstract methods.

A functional interface can be implicitly converted to a lambda expression.

A functional interface can support lambda in an existing function-friendly manner.

Pre-JDK 1.8 functional Interfaces:

    • Java.lang.Runnable
    • Java.util.concurrent.Callable
    • Java.security.PrivilegedAction
    • Java.util.Comparator
    • Java.io.FileFilter
    • Java.nio.file.PathMatcher
    • Java.lang.reflect.InvocationHandler
    • Java.beans.PropertyChangeListener
    • Java.awt.event.ActionListener
    • Javax.swing.event.ChangeListener

New added function interface for JDK 1.8:

    • Java.util.function

Java.util.function it contains a number of classes that are used to support functional programming in Java, the function interface in the package is:

Serial Number Interface & Description
1 Biconsumer<t,u>: represents an operation that accepts two input parameters and does not return any results
2 Bifunction<t,u,r>: represents a method that accepts two input parameters and returns a result
3 Binaryoperator<t>: represents an operation that acts on two homogeneous operators, and returns the result of the same type of operator
4 Bipredicate<t,u>: A Boolean method that represents a two-parameter
5 Booleansupplier: A provider that represents the result of a Boolean value
6 Consumer<t>: represents an operation that takes an input parameter and has no return
7 Doublebinaryoperator: represents an operation that acts on a two double value operator and returns the result of a double value.
8 Doubleconsumer: represents an operation that accepts a double value parameter and does not return a result.
9 Doublefunction<r>: represents a method that accepts a double value parameter and returns the result
10 Doublepredicate: Represents a Boolean value method that has a double value argument
11 Doublesupplier: represents a provider of a double value structure
12 doubletointfunction: accepts a double type input and returns an int type result.
13 doubletolongfunction: accepts a double type input, returns a long type result
14 Doubleunaryoperator: accepts a parameter of the same type double, and the return value type is also double.
15 Function<t,r>: accepts an input parameter and returns a result.
16 Intbinaryoperator: accepts two parameters of the same type int, and the return value type is also int.
17 Intconsumer: accepts an input parameter of type int, no return value.
18 Intfunction<r>: accepts an int type input parameter and returns a result.
19 intpredicate: takes an int input parameter and returns the result of a Boolean value.
20 intsupplier: No parameter, returns an int type result.
21st inttodoublefunction: accepts an int type input and returns a double type result.
22 inttolongfunction: accepts an int type input and returns a long type result.
23 Intunaryoperator: accepts a parameter of the same type int, and the return value type is also int.
24 Longbinaryoperator: accepts two parameters of the same type long, and the return value type is also long.
25 Longconsumer: accepts a long type of input parameter with no return value.
26 Longfunction<r>: accepts a long type input parameter and returns a result.
27 longpredicate: R takes a long input parameter and returns a Boolean value type result.
28 longsupplier: No parameter, returns a result long type value.
29 longtodoublefunction: accepts a long type input and returns a double type result.
30 longtointfunction: accepts a long type input and returns an int type result.
31 Longunaryoperator: accepts a parameter of the same type long, and the return value type is also long.
32 Objdoubleconsumer<t>: accepts an object type and an input parameter of type double, no return value.
33 Objintconsumer<t>: accepts an object type and an input parameter of type int, no return value.
34 Objlongconsumer<t>: accepts an object type and an input parameter of type long, no return value.
35 Predicate<t>: accepts an input parameter and returns a Boolean result.
36 supplier<t>: no parameter, returns a result.
37 Todoublebifunction<t,u>: accepts two input parameters, returns a double type result
38 Todoublefunction<t>: accepts an input parameter and returns a double type result
39 Tointbifunction<t,u>: accepts two input parameters and returns an int type result.
40 Tointfunction<t>: accepts an input parameter and returns an int type result.
41 Tolongbifunction<t,u>: accepts two input parameters, returns a long type result.
42 Tolongfunction<t>: accepts an input parameter and returns a long type result.
43 Unaryoperator<t>: accepts a parameter of type T, and the return value type is also T.

Functional Interface Instances

1.Predicate

 PackageJava8.cainiao;Importjava.util.Arrays;Importjava.util.List;Importjava.util.function.Predicate; Public classPredicatetest { Public Static voidMain (String args[]) {List<Integer> list = Arrays.aslist (1, 2, 3, 4, 5, 6, 7, 8, 9); //predicate<integer> predicate = n-True//N is a test method that passes a parameter to the predicate interface//N If present, the test method returns TrueSystem.out.println ("Output All data:"); //Pass parameter nEval (list, n->true); //predicate<integer> predicate1 = n-n%2 = = 0//N is a test method that passes a parameter to the predicate interface//returns True if n%2 is 0 test methodSystem.out.println ("Output all even numbers:"); eval (list, nn%2 = = 0 ); //predicate<integer> predicate2 = n > 3//N is a test method that passes a parameter to the predicate interface//if n is greater than 3 the test method returns TrueSYSTEM.OUT.PRINTLN ("Output all numbers greater than 3:"); eval (list, nn > 3 ); }        Public Static voidEval (list<integer> List, predicate<integer>predicate) {       for(Integer n:list) {if(Predicate.test (n)) {System.out.println (n+ " "); }      }   }}

Java 8 new Features-Rookie tutorial (3)-java 8 function interface

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.