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