I can't understand the method Comparable in Map when I look at the internal interface of Entry < KJI V >.

when you look at the interface of Java"s source code Map, Entry < K _ Magi V >, you can"t understand how to write the methods in it

    public static <K extends Comparable<? super K>, V> Comparator<Map.Entry<K,V>> comparingByKey() {
        return (Comparator<Map.Entry<K, V>> & Serializable)
            (c1, c2) -> c1.getKey().compareTo(c2.getKey());
    }

I can"t understand this writing. Ask for explanation!

Jun.16,2022

(Comparator < Map.Entry < K, V > > & Serializable) means to cast the result to a Comparator object that implements the Serializable interface
this is the syntax of Java8, and means to cast to multiple types

at the same time.

(C1, c2)-> c1.getKey () .compareTo (c2.getKey ()) is equivalent to anonymous inner class:

new Comparator<T>(){
    @Override
    public int compare(T c1, T c2) {
        return c1.getKey().compareTo(c2.getKey());
    }
}

is also the syntax of Java8, Lambda expression

Menu