LoginSignup
3
2

More than 5 years have passed since last update.

java8 lambda入門編

Last updated at Posted at 2018-04-11

java8 lambda 式とは

実装が必要なメソッドを1つだけ持つインターフェースに、
実現内容を簡単に書ける式。

例1:local classを利用した場合

    public static void main(String[] args) {

        // local class を利用した場合
        class LocalImp implements Runnable {
            public void run() {
                System.out.println("hello lambda!");
            }
        }

        Runnable runner = new LocalImp();
        runner.run(); // hello lambda!

    }

例2:無名classを利用した場合

    public static void main(String[] args) {

        // 無名class を利用した場合
        Runnable runner = new Runnable() {
            public void run() {
                System.out.println("hello lambda!");
            }
        };

        runner.run(); // hello lambda!

    }

例3:lambda式を利用した場合

    public static void main(String[] args) {

        // lambdaを利用した場合
        Runnable runner = () -> {System.out.println("hello lambda!");};

        runner.run(); // hello lambda!

    }

変数のスコープ

匿名クラスの使い方と同様、
外側で定義された変数を使用する場合、final もしくは実質的 final である必要がある。
(java8は、final定義が省略できるが、実質的finalが必要)

例1:final変数の参照

    public static void main(String[] args) {

        final int count = 1;

        Runnable runner = () -> {
            System.out.println("count is " + count);
        };

        runner.run(); // count is 1

例2:finalを省略する場合

    public static void main(String[] args) {

        int count = 1;

        Runnable runner = () -> {
            System.out.println("count is " + count);
        };

        runner.run(); // count is 1

    }

例3:実質finalではない場合、コンパイルエラー

    public static void main(String[] args) {

        int count = 1;
        count ++; //実質finalではない

        Runnable runner = () -> {
            System.out.println("count is " + count);
            // コンパイルエラー:Local variable count defined in an enclosing scope must be final or effectively final
        };

        runner.run();

    }

例4:外部参照を変更しよう場合、コンパイルエラー

    public static void main(String[] args) {

        int count = 1;

        Runnable runner = () -> {
            System.out.println("count is " + count);

            count ++;
            // コンパイルエラー:Local variable count defined in an enclosing scope must be final or effectively final
        };

        runner.run();

    }

stream APIとは

lambda式で、要素の集まりに対して様々な処理を行うAPI

処理の流れ

結果 = stream生成().中間操作A().中間操作A().中間操作A().終端操作();

・中間操作:
   戻り値がない、内容を編集するだけ。
・終端操作:
   戻り値があり。

よく使うものはこれを参照:
Java8 Streamざっくりまとめ

関数型インターフェイス

java se8で追加されたlambdaで使うパッケージ:
  公式:https://docs.oracle.com/javase/jp/8/docs/api/java/util/function/package-summary.html

下記はよく使われる例;

・ Consumer<T>

T: 引数
interface: void accept(T)

Consumer<Goods> buyer = (goods) -> { System.out.println(goods.getName() + "を買いました。"); };

buyer.accept(new Goods("コーラ")); // コーラを買いました。

・ Function<T, R>

T: 引数
R: 戻り値
interface: R apply(T)

        Function<Integer, String> onePlus = (i) -> {
            return "結果は:" + (i + 1);
        };

        System.out.println(onePlus.apply(5)); // 結果は:6

・ Predicate<T>

T: 引数
interface: boolean test(T)
※テスト用かな。。。

        Predicate<String> isNull = (o) -> {
            return  null == o ;
        };

        boolean result = isNull.test("notNull");

        System.out.println(result); // false
3
2
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
3
2