标签:队列 print block group seq sequence 优雅 消费 sorted
Java版本现在已经发布到JDK13了,目前公司还是用的JDK8,还是有必要了解一些JDK8的新特性的,例如优雅判空的Optional类,操作集合的Stream流,函数式编程等等;这里就按操作例举一些常用的Stream流操作;
A sequence of elements supporting sequential and parallel aggregate operations. Stream流是一个来自数据源的元素队列并支持聚合操作
List<Integer> list = Arrays.asList(6, 5, 5, 1, 4);
list.stream().peek(i -> System.out.println(i)).forEach(i-> System.out.println(i));
int[] nums={1,2,3,5,6}; int sum = IntStream.of(nums).map(i->i*2).sum();
List<Integer> list = Arrays.asList(6, 5, 5, 1, 4);
list.stream().filter(integer -> integer>1).forEach(System.out::println);
List<Integer> list = Arrays.asList(6, 5, 5, 1, 4);
List<Integer> collect = list.stream().sorted((o1, o2) -> o1 - o2).collect(Collectors.toList());
List<Integer> list = Arrays.asList(6, 5, 5, 1, 4);
list.stream().dinstinct().forEach(System.out::println)
List<Integer> list = Arrays.asList(6, 5, 5, 1, 4);
list.stream().limit(1).forEach(System.out::println);
List<Integer> list = Arrays.asList(1, 2, 3, 5, 6); long count = list.stream().count();
List<Integer> list = Arrays.asList(1, 2, 3, 5, 6);
Integer reduce = list.stream().reduce((integer, integer2) -> integer + integer2).get();
List<Integer> list = Arrays.asList(1, 2, 3, 5, 6);
list.stream().forEach(System.out::println);
List<Integer> list = Arrays.asList(1, 2, 3, 5, 6);
Integer max = list.stream().max((s1, s2) -> s1 - s2).get();
List<Integer> list = Arrays.asList(1, 2, 3, 5, 6);
Integer min = list.stream().max((o1, o2) -> o2 - o1).get();
List<Integer> list = Arrays.asList(1, 2, 3, 5, 6); boolean b = list.stream().noneMatch((x) -> x.intValue() == 1); boolean b = list.stream().allMatch((x) -> x.intValue() == 1); boolean b = list.stream().anyMatch((x) -> x.intValue() == 1);
List<Integer> list = Arrays.asList(1, 2, 3, 5, 6); //求平均数 Double average= list.stream().collect(Collectors.averagingInt(x -> x.intValue())); //求分组 Double average= list.stream().collect(Collectors.groupinngby(x -> x.intValue())); //收集成一个List List<Integer> collect = list.stream().sorted((o1, o2) -> o1 - o2).collect(Collectors.toList());
Double sum_= list.stream().collect(Collectors.summarizingInt(value -> value.intValue())).getAve
标签:队列 print block group seq sequence 优雅 消费 sorted
原文地址:https://www.cnblogs.com/gabriel-y/p/11657390.html