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 Interfa…


This content originally appeared on DEV Community and was authored by Wagner Negrão

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.


This content originally appeared on DEV Community and was authored by Wagner Negrão


Print Share Comment Cite Upload Translate Updates
APA

Wagner Negrão | Sciencx (2021-07-12T21:23:37+00:00) What are the Interface predicate and Interface functions?. Retrieved from https://www.scien.cx/2021/07/12/what-are-the-interface-predicate-and-interface-functions/

MLA
" » What are the Interface predicate and Interface functions?." Wagner Negrão | Sciencx - Monday July 12, 2021, https://www.scien.cx/2021/07/12/what-are-the-interface-predicate-and-interface-functions/
HARVARD
Wagner Negrão | Sciencx Monday July 12, 2021 » What are the Interface predicate and Interface functions?., viewed ,<https://www.scien.cx/2021/07/12/what-are-the-interface-predicate-and-interface-functions/>
VANCOUVER
Wagner Negrão | Sciencx - » What are the Interface predicate and Interface functions?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/12/what-are-the-interface-predicate-and-interface-functions/
CHICAGO
" » What are the Interface predicate and Interface functions?." Wagner Negrão | Sciencx - Accessed . https://www.scien.cx/2021/07/12/what-are-the-interface-predicate-and-interface-functions/
IEEE
" » What are the Interface predicate and Interface functions?." Wagner Negrão | Sciencx [Online]. Available: https://www.scien.cx/2021/07/12/what-are-the-interface-predicate-and-interface-functions/. [Accessed: ]
rf:citation
» What are the Interface predicate and Interface functions? | Wagner Negrão | Sciencx | https://www.scien.cx/2021/07/12/what-are-the-interface-predicate-and-interface-functions/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.