functional interfaces have a single functionality to exhibit. for example, a comparable interface with a single method ‘compareto’ is used for comparison purpose. java 8 has defined a lot of functional interfaces to be used extensively in lambda expressions. following is the list of functional interfaces defined in java.util.function package.
sr.no. | interface & description |
---|---|
1 |
biconsumer<t,u> represents an operation that accepts two input arguments, and returns no result. |
2 |
bifunction<t,u,r> represents a function that accepts two arguments and produces a result. |
3 |
binaryoperator<t> represents an operation upon two operands of the same type, producing a result of the same type as the operands. |
4 |
bipredicate<t,u> represents a predicate (boolean-valued function) of two arguments. |
5 |
booleansupplier represents a supplier of boolean-valued results. |
6 |
consumer<t> represents an operation that accepts a single input argument and returns no result. |
7 |
doublebinaryoperator represents an operation upon two double-valued operands and producing a double-valued result. |
8 |
doubleconsumer represents an operation that accepts a single double-valued argument and returns no result. |
9 |
doublefunction<r> represents a function that accepts a double-valued argument and produces a result. |
10 |
doublepredicate represents a predicate (boolean-valued function) of one double-valued argument. |
11 |
doublesupplier represents a supplier of double-valued results. |
12 |
doubletointfunction represents a function that accepts a double-valued argument and produces an int-valued result. |
13 |
doubletolongfunction represents a function that accepts a double-valued argument and produces a long-valued result. |
14 |
doubleunaryoperator represents an operation on a single double-valued operand that produces a double-valued result. |
15 |
function<t,r> represents a function that accepts one argument and produces a result. |
16 |
intbinaryoperator represents an operation upon two int-valued operands and produces an int-valued result. |
17 |
intconsumer represents an operation that accepts a single int-valued argument and returns no result. |
18 |
intfunction<r> represents a function that accepts an int-valued argument and produces a result. |
19 |
intpredicate represents a predicate (boolean-valued function) of one int-valued argument. |
20 |
intsupplier represents a supplier of int-valued results. |
21 |
inttodoublefunction represents a function that accepts an int-valued argument and produces a double-valued result. |
22 |
inttolongfunction represents a function that accepts an int-valued argument and produces a long-valued result. |
23 |
intunaryoperator represents an operation on a single int-valued operand that produces an int-valued result. |
24 |
longbinaryoperator represents an operation upon two long-valued operands and produces a long-valued result. |
25 |
longconsumer represents an operation that accepts a single long-valued argument and returns no result. |
26 |
longfunction<r> represents a function that accepts a long-valued argument and produces a result. |
27 |
longpredicate represents a predicate (boolean-valued function) of one long-valued argument. |
28 |
longsupplier represents a supplier of long-valued results. |
29 |
longtodoublefunction represents a function that accepts a long-valued argument and produces a double-valued result. |
30 |
longtointfunction represents a function that accepts a long-valued argument and produces an int-valued result. |
31 |
longunaryoperator represents an operation on a single long-valued operand that produces a long-valued result. |
32 |
objdoubleconsumer<t> represents an operation that accepts an object-valued and a double-valued argument, and returns no result. |
33 |
objintconsumer<t> represents an operation that accepts an object-valued and an int-valued argument, and returns no result. |
34 |
objlongconsumer<t> represents an operation that accepts an object-valued and a long-valued argument, and returns no result. |
35 |
predicate<t> represents a predicate (boolean-valued function) of one argument. |
36 |
supplier<t> represents a supplier of results. |
37 |
todoublebifunction<t,u> represents a function that accepts two arguments and produces a double-valued result. |
38 |
todoublefunction<t> represents a function that produces a double-valued result. |
39 |
tointbifunction<t,u> represents a function that accepts two arguments and produces an int-valued result. |
40 |
tointfunction<t> represents a function that produces an int-valued result. |
41 |
tolongbifunction<t,u> represents a function that accepts two arguments and produces a long-valued result. |
42 |
tolongfunction<t> represents a function that produces a long-valued result. |
43 |
unaryoperator<t> represents an operation on a single operand that produces a result of the same type as its operand. |
functional interface example
predicate <t> interface is a functional interface with a method test(object) to return a boolean value. this interface signifies that an object is tested to be true or false.
create the following java program using any editor of your choice in, say, c:\> java.
java8tester.java
import java.util.arrays; import java.util.list; import java.util.function.predicate; public class java8tester { public static void main(string args[]) { list<integer> list = arrays.aslist(1, 2, 3, 4, 5, 6, 7, 8, 9); // predicate<integer> predicate = n -> true // n is passed as parameter to test method of predicate interface // test method will always return true no matter what value n has. system.out.println("print all numbers:"); //pass n as parameter eval(list, n->true); // predicate<integer> predicate1 = n -> n%2 == 0 // n is passed as parameter to test method of predicate interface // test method will return true if n%2 comes to be zero system.out.println("print even numbers:"); eval(list, n-> n%2 == 0 ); // predicate<integer> predicate2 = n -> n > 3 // n is passed as parameter to test method of predicate interface // test method will return true if n is greater than 3. system.out.println("print numbers greater than 3:"); eval(list, n-> n > 3 ); } public static void eval(list<integer> list, predicate<integer> predicate) { for(integer n: list) { if(predicate.test(n)) { system.out.println(n + " "); } } } }
here we've passed predicate interface, which takes a single input and returns boolean.
verify the result
compile the class using javac compiler as follows −
c:\java>javac java8tester.java
now run the java8tester as follows −
c:\java>java java8tester
it should produce the following output −
print all numbers: 1 2 3 4 5 6 7 8 9 print even numbers: 2 4 6 8 print numbers greater than 3: 4 5 6 7 8 9