ListやArrayの各要素を委譲処理する仕組み
Stream interfaceに用意されたmethodを使用して処理する
Collection#stream()でfactoryする。
sorted, filter is intermidiate operation
forEach is terminal operation
primitive type は、IntStream, LongStream etc を使用
public class Outer {
public static void main(String[] args) {
List<Integer> l = List.of(4,2,3);
Stream<Integer> s = l.stream();
s.sorted().forEach(x -> System.out.println(x));
s = l.stream();
s.filter(x -> x % 2 == 0).forEach(x -> System.out.println(x));
int[] ia= {1,2,3};
IntStream is = Arrays.stream(ia);
is.forEach(System.out::println);
Set<String> st = new HashSet<>();
st.add("c");
st.add("x");
st.add("b");
Stream<String> ss = st.stream();
ss.forEach(System.out::println);
}
}
2
3
4
4
2
1
2
3
b
c
x
paralellStream sample
multi thread process
forEachOrderedは順番通り、forEachは順不同
public class Outer {
public static void main(String[] args) {
List<Integer> l = List.of(1,2,3);
l.<String>parallelStream().forEach(System.out::println);
l.<String>parallelStream().forEachOrdered(System.out::println);
}
}
2
3
1
1
2
3