Java 8 - Interfaces Funcionais
As interfaces funcionais têm uma única funcionalidade para exibir. Por exemplo, uma interface Comparable com um único método 'compareTo' é usada para fins de comparação. Java 8 definiu várias interfaces funcionais para serem usadas extensivamente em expressões lambda. A seguir está a lista de interfaces funcionais definidas no pacote java.util.Function.
| Sr. Não. | Interface e descrição |
|---|---|
| 1 | BiConsumer<T,U> Representa uma operação que aceita dois argumentos de entrada e não retorna nenhum resultado. |
| 2 | BiFunction<T,U,R> Representa uma função que aceita dois argumentos e produz um resultado. |
| 3 | BinaryOperator<T> Representa uma operação sobre dois operandos do mesmo tipo, produzindo um resultado do mesmo tipo que os operandos. |
| 4 | BiPredicate<T,U> Representa um predicado (função com valor booleano) de dois argumentos. |
| 5 | BooleanSupplier Representa um fornecedor de resultados com valor booleano. |
| 6 | Consumer<T> Representa uma operação que aceita um único argumento de entrada e não retorna nenhum resultado. |
| 7 | DoubleBinaryOperator Representa uma operação sobre dois operandos de valor duplo e produzindo um resultado de valor duplo. |
| 8 | DoubleConsumer Representa uma operação que aceita um único argumento de valor duplo e não retorna nenhum resultado. |
| 9 | DoubleFunction<R> Representa uma função que aceita um argumento de valor duplo e produz um resultado. |
| 10 | DoublePredicate Representa um predicado (função de valor booleano) de um argumento de valor duplo. |
| 11 | DoubleSupplier Representa um fornecedor de resultados de valor duplo. |
| 12 | DoubleToIntFunction Representa uma função que aceita um argumento com valor duplo e produz um resultado com valor int. |
| 13 | DoubleToLongFunction Representa uma função que aceita um argumento de valor duplo e produz um resultado de valor longo. |
| 14 | DoubleUnaryOperator Representa uma operação em um único operando de valor duplo que produz um resultado de valor duplo. |
| 15 | Function<T,R> Representa uma função que aceita um argumento e produz um resultado. |
| 16 | IntBinaryOperator Representa uma operação sobre dois operandos com valor int e produz um resultado com valor int. |
| 17 | IntConsumer Representa uma operação que aceita um único argumento com valor int e não retorna nenhum resultado. |
| 18 | IntFunction<R> Representa uma função que aceita um argumento com valor int e produz um resultado. |
| 19 | IntPredicate Representa um predicado (função com valor booleano) de um argumento com valor int. |
| 20 | IntSupplier Representa um fornecedor de resultados com valor int. |
| 21 | IntToDoubleFunction Representa uma função que aceita um argumento com valor int e produz um resultado com valor duplo. |
| 22 | IntToLongFunction Representa uma função que aceita um argumento com valor int e produz um resultado com valor longo. |
| 23 | IntUnaryOperator Representa uma operação em um único operando com valor interno que produz um resultado com valor interno. |
| 24 | LongBinaryOperator Representa uma operação sobre dois operandos de valor longo e produz um resultado de valor longo. |
| 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