1
0

More than 1 year has passed since last update.

ラムダ式を勉強する

Posted at

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
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0