I was looking into the source code of Comparator#comparing()
from JDK 1.8, and the casting of lambda was a surprise for me:
//declaration
{
Objects.requireNonNull(keyExtractor);
return (Comparator<T> & Serializable) // <- this cast here
(c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2));
}
After reading that
The returned comparator is serializable if the specified function is also serializable.
and some playing with the code, I figured out why this works and why it wouldn't work if the cast was different.
My question is: What is this (A & B)
multiple casting?
By googling java multiple cast
I found only multiple casting using (A)(B)
. Is there like an article about adding this in JDK 1.8
?