Looking at java.util.Comparator
in the JDK 8 source code, I found interesting code:
public static <T, U extends Comparable<? super U>> Comparator<T> comparing(Function<? super T, ? extends U> keyExtractor){
Objects.requireNonNull(keyExtractor);
// casting to Serializable?
return (Comparator<T> & Serializable)
(c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2));
}
It is pretty clear why we are casting the resulting lambda to Comparator<T>
, but what is the purpose of casting it to Serializable
?