0
0

More than 1 year has passed since last update.

stream forEach sorted method

Last updated at Posted at 2023-01-26

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
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