40
What are the Interface predicate and Interface functions?
Writing the post about Consumer Interface, I thought it interesting to write about Predicate Interface and Function Interface.
We go to start with Predicate Interface, this interface is an interface functional a little different from Consumer Interface that receives a parameter and does not return anything. The Predicate interface receives a parameter and returns a value boolean. The function that an interface receives is primarily some comparison that can have with returns a boolean.
see an example:
List<Integer> numbers = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
numbers.stream().filter((c) -> (c % 2) == 0)
In the example above, we have a creation of a list and after that is created a filter to know what the pair numbers are. I'm using this example because the method 'filter' receives an interface predicate and stays easier to explain the example.
Thus to receive a Predicate, he needs to pass a condition of comparison, in the example we have that do 'c%2' to know the rest of division, if it equals 0, this will generate an answer of true or false, thus satisfy the conditions of use of Predicate Interface. When performing a filter he will pass forward only what is true and eliminate from the stream all that is false. It's good to highlight that the list will not modify the filter but the stream that was created.
List<Integer> numbers = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
numbers.stream()
.filter((c) -> (c % 2) == 0)
.forEach(System.out::println);
Result:
2
4
6
8
10
The results presented are the number pairs after being filtered.
Now I will speak a little about the Function interface.
The Function Interface é some more generics and well malleable to work with, which we see below.
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
How we can see inside of the interface, he receives a type T and returns a type R. This return does not need to necessarily return a type that was passed, soon it can be used for transformation.
See an example:
List<String> numbers = Arrays.asList("1","2","3");
numbers.stream()
.map(c -> new Integer(c))
.forEach(k -> System.out.println(k + " - Type: " + k.getClass().getSimpleName()));
Result:
1 - Type: Integer
2 - Type: Integer
3 - Type: Integer
In the example above we have a list of strings, after creating a stream and used the function 'Map' to convert the values of the string to Integer and a present in CLI the variable and the be the type.
We used the method Map of stream because to operate it must receive the Functional Interface, that is why we were able to realize the transformation of string to Integer. We see step by step, the method 'Apply' of interface receives a parameter and has that execute an action and return something, that in we case would return an Integer, for this reason, the method Map is known for a method that if used to transform items of a stream.
This example presented is well simple, but I believe that it suits to understand possible applications using the Function Interface or mainly the Map() that facilitate quiet work with the transformation of objects.
I must emphasize that in we case map(), creating the new stream, all of them being integers, and this does not influence anything the stream principal, he continued being of String type.
We can leave this code more elegant using methods reference, but this will be the subject of other posts.
40