2021/2/21

1、流式计算Stream

  • java.util.Stream
  • eg:
public class StreamTest {
    public static void main(String[] args) {
        ArrayList<User> users = new ArrayList<>();
        users.add(new User("wang",12));
        users.add(new User("li",123));
        users.add(new User("ng",112));
        users.add(new User("ang",1));
        Stream<User> stream = users.stream();
        stream.sorted((u1,u2)->{return u1.age - u2.age;})
                .map(u->{return u.age;})
                .forEach(System.out::println);
    }
}
class User{
    String name;
    int age;
    User(String s,int a){
        this.name = s;
        this.age = a;
    }
}
输出结果:
1
12
112
123

2、stream并行流计算1到10亿(底层是forkjoin-工作窃取)

  • Long result = LongStream.rangeClosed(1, 10_0000_0000).parallel().reduce(0,Long::sum);
posted @ 2021-02-21 12:10  天气晴21℃  阅读(71)  评论(0编辑  收藏  举报