LoginSignup
0

More than 5 years have passed since last update.

Java8のお勉強(Stream)

Last updated at Posted at 2017-10-15

今回はStreamです。
早速サンプル。

import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * Streamのお勉強。Intのストリーム。
 * @author komikcomik
 *
 */
public class StreamSample {
    public static void main(String[] args) {

        // ストリームを作って、中で一連の処理をやってもらうみたいな感じ
        IntStream is = IntStream.of(1000, 123, 456, 1001, 10000, 919, 561, 2000);
        // 1000以上のデータを抽出して、ソートする、その後標準出力
        is.filter(i -> i >= 1000).sorted().forEach(i -> System.out.println(i));

        // 1文字でないデータを除去→ソート→標準出力
        Stream<String> strStream = Stream.of("A", "b", "C", "d", "AAA", "BBB", "E");
        strStream.filter(s -> s.length() == 1).sorted().forEach(s -> System.out.println(s));
    }
}

実行結果

1000
1001
2000
10000
A
C
E
b
d

イメージは涌きやすかったですがこれをガンガン現場で使えるかって言ったら結構悩みそうですね。

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