abstract methodを1つだけもつインタフェース。
new with implementして、overrideする。
Comparator#compareをoverride
public static void main(String[] args) {
Comparator<String> c = new ComparatorImpl();
System.out.println(c.compare("ABC", "DEF"));
}
static class ComparatorImpl implements Comparator<String> {
public int compare(String s1, String s2) {
return s1.compareTo(s2);
}
}
-3
ラムダ式を使う。
Comparatorのabstract methodはcompareの1つのみのため、
メソッド名compare, argument type, return,new を省略する。
残るのはargs、method contentのみ
public static void main(String[] args) {
Comparator<String> c = (s1,s2) -> s1.compareTo(s2);
System.out.println(c.compare("ABC", "DEF"));
}
-3