これはなに

最初の Stream

package com.mycompany.sandbox;

import java.util.ArrayList;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
class Person {
    private String name;
    private int age;
}

public class StreamExam {
    public static void main(String[] args) {
        List<Person> list = new ArrayList<>();
        list.add(new Person("Zach", 18));
        list.add(new Person("Albert", 59));
        list.add(new Person("Donna", 38));
        list.add(new Person("Erich", 40));
        list.add(new Person("Brenda", 16));
        
        list.stream()
            .sorted((a,b) -> a.getName().compareTo(b.getName()))
            .forEach(p -> System.out.println(p));
    }    
}
Person(name=Albert, age=59)
Person(name=Brenda, age=16)
Person(name=Donna, age=38)
Person(name=Erich, age=40)
Person(name=Zach, age=18)

Stream を作る

中間操作

重複を排除した Stream を返す

IntStream.of(1,2,3,3)
    .distinct()
    .forEach(System.out::println);
1
2
3

フィルタリングした Stream を返す

IntStream.of(1,2,3,3)
    .filter(i -> i > 2)
    .forEach(System.out::println);
3
3

Stream を連結した Stream を返す

IntStream.concat(IntStream.range(0,3), IntStream.range(100,103))
    .forEach(System.out::println);
0
1
2
100
101
102

要素を変換した Stream を返す

IntStream.of(1,2,3,3)
    .map(i -> i * 2)
    .forEach(System.out::println);
2
4
6
6

要素を変換した Stream を連結した Stream を返す

IntStream.of(1,2,3,3)
    .flatMap(i -> IntStream.of(i, i * 2))
    .forEach(System.out::println);
1
2
2
4
3
6
3
6

mapToDouble?

DoubleStream? に変換する

mapToInt?

IntStream? に変換する

mapToLong?

LongStrea? に変換する

ソートする

IntStream.of(3,2,1,3)
    .sorted()
    .forEach(System.out::println);
1
2
3
3

最初から 2 個の Stream を返す

IntStream.of(1,2,3,3)
    .limit(2)
    .forEach(System.out::println);
1
2

終端操作

条件に合致するか調べる

System.out.println(IntStream.of(1,2,3,3).allMatch(i -> i > 0));
System.out.println(IntStream.of(1,2,3,3).allMatch(i -> i > 1));
System.out.println("--");
System.out.println(IntStream.of(1,2,3,3).anyMatch(i -> i > 1));
System.out.println(IntStream.of(1,2,3,3).anyMatch(i -> i > 3));
System.out.println("--");
System.out.println(IntStream.of(1,2,3,3).noneMatch(i -> i > 1));
System.out.println(IntStream.of(1,2,3,3).noneMatch(i -> i > 3));
System.out.println("--");
true
false
--
true
false
--
false
true
--

要素数を数える

System.out.println(IntStream.of(1,2,3,3).count());
4

要素数を合計する

System.out.println(IntStream.of(1,2,3,3).sum());
9

要素数を指定した方法で集計する (1*1*2*3*3)

System.out.println(IntStream.of(1,2,3,3).reduce(1, (a,b) -> a * b));
18

各要素に任意の処理を行う

IntStream.of(1,2,3,3)
    .forEach(System.out::println);
1
2
3
3

Collect

最大、最小、区切り文字で連結

System.out.println(Stream.of(1,2,3,3).collect(Collectors.minBy((a,b) -> a.compareTo(b))));
System.out.println(Stream.of(1,2,3,3).collect(Collectors.maxBy((a,b) -> a.compareTo(b))));
System.out.println(Stream.of("1","2","3","3").collect(Collectors.joining(",")));
Optional[1]
Optional[3]
1,2,3,3

Listに変換する

List<Integer> set = Stream.of(1,2,3,3).collect(Collectors.toList());
System.out.println(list);
[1, 2, 3]

Setに変換する

Set<Integer> set = Stream.of(1,2,3,3).collect(Collectors.toSet());
System.out.println(set);
[1, 2, 3]

Mapに変換する

Map<Character, String> map = Stream.of("Zach","Albert","Donna","Erich","Brenda")
    .collect(Collectors.toMap(
        s -> s.charAt(0),
        s -> s
));
System.out.println(map);
{A=Albert, B=Brenda, D=Donna, E=Erich, Z=Zach}

Mapにグルーピング

Map<Integer, List<String>> map2 = Stream.of("Zach","Albert","Donna","Erich","Brenda")
    .collect(Collectors.groupingBy(
        s -> s.length()
));
System.out.println(map2);
{4=[Zach], 5=[Donna, Erich], 6=[Albert, Brenda]}

並列実行

double pi = LongStream.range(0L, 10_000_000L)
        .parallel()
        .map(i -> ((i & 1) == 0 ? 1 : -1) * (2 * i + 1))
        .mapToDouble(i -> 4.0 / i)
        .sum();
System.out.println(pi);
3.1415925535897933

Java#JavaSE


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS   sitemap
Last-modified: 2014-05-26 (月) 02:27:21 (3616d)
Short-URL:
ISBN10
ISBN13
9784061426061