Looking for how to order a list of objects by a parameter I found this solution:
lista.sort(
Comparator.comparing(EntradaLoteVo::getFechaRecepcionLote)
.thenComparingInt(EntradaLoteVo::getId));
And this catches my attention: EntradaLoteVo::getFechaRecepcionLote
. Is it a lambda expression? I have not been able to find explanations on the internet because I do not know what it is called. I think it returns the value that I have in the list that corresponds to the class EntradaLoteVo
and executes the operation getFechaRecepcionLote
for each element and then sort
sorts it. But what else can you do?
::
is called a Method Reference. It was introduced in Java 8.For a long time, interfaces were used as callbacks for asynchronous processes, such as when you wanted to get the response from the server or when you wanted to create a new thread:
Now you can do something similar by sending method references to use as callbacks and avoid such complex syntax:
(valido, token) -> {}
It is a valid method that translated would be:Which, adapting it to your question, would be:
What this does is send the method reference
callback_respuesta
to the methodloginUsuario
.This is called lambda functions. There are 3 types:
Function
,Supplier
andConsummer
. It's good to note that in the end all 3 are interfaces which is a trick the compiler does to accept that type of syntax.This is the same as the
Func<T, T1, T2.., TResult>
andAction<T, T2...>
's in C#.