0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ラムダ式・ストリームについて自分の見解とまとめ

Last updated at Posted at 2021-10-02
  • ラムダ式の引数戻り値には、型の指定を行わない
  • java8から「型推論」の機能が追加されたから
  • 単一関数インターフェース(java.util.function)を使う実装につかえる
  • 引数が一つの場合は()を省略可能、引数がない場合は()が必要

IntBinaryOperetor op = (x, y) -> x + y;

例 型を書くこともいちお可能

IntBinaryOperetor op = (int x, int y) -> {x + y;};
  • メソッド参照について
    引数に対してシンプルにメソッドを適用する場合はメソッド参照でさらに簡潔に記述することができる
Function<String, Integer> func = str -> str.length();

下のように書き換えが可能である

Function<String, Integer> func = str -> String::length;

偶数をカウントするプログラム(ラムダ式)

List<Integer> list = Arrays.asList(5,8,1,2,3);
long count = list.stream().filter(x -> x % 2 == 0).count();

ストリームについて(java.util.stream)

  • データ集合(配列やコレクションのこと)に対する処理をラムダ式で簡潔に記述する為の新しい概念

以下のようにArraysはstreamメソッドを持っているので、streamオブジェクト
を取得できる

List<String> list = Arrays.asList("A", "B", "C");
Stream<String> stream = list.stream();
  • 基本的な使い方

    ①ストリームオブジェクトの取得

    ②中間操作メソッド(ラムダ式)

    ③終端操作メソッド();
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?