JDK新特性关于流操作部分

    // array 工具类   可以用来快捷的将数组转化为list
List<String> strings = Arrays.asList("zhongguo", "riben", "yingguo");
// 转化为一个新的list
List<String> collect = strings.stream().map(number -> number += "dfvgcdvcx").collect(Collectors.toList());
// 流输出
collect.forEach(num -> System.out.println( num));
// 帅选偶数
List<Integer> numberList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
List<Integer> collect1 = numberList.stream().filter(num -> num % 2 == 0).collect(Collectors.toList());
collect1.forEach(number -> System.out.println(number));
// 从无线流数据中选择第一个
List<Integer> numberList1= Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
Optional<Integer> first = numberList1.stream().findFirst();
// 输出第一个
System.out.println(first.orElse(-1));
// 获取或者扔掉前 n 个元素
List<Integer> ageList = Arrays.asList(11, 22, 13, 14, 25, 26);
Stream<Integer> limit = ageList.stream().limit(4);
limit.forEach(li -> System.out.println(li));
Stream<Integer> skip = ageList.stream().skip(4);
skip.forEach(sk-> System.out.println(sk));
// 数学统计功能
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6);
IntSummaryStatistics intSummaryStatistics = list.stream().mapToInt(x -> x).summaryStatistics();
System.out.println(intSummaryStatistics.getMax());
System.out.println(intSummaryStatistics.getMin());
System.out.println(intSummaryStatistics.getAverage());
System.out.println(intSummaryStatistics.getCount());
System.out.println(intSummaryStatistics.getSum());
// 分组聚合功能
List<Integer> groupList = Arrays.asList(11, 22, 13, 14, 25, 26);
Map<Integer, List<Integer>> collect2 = groupList.stream().collect(Collectors.groupingBy(age -> age / 10));
collect2.forEach((k,v) -> System.out.println(k+"nmnmn"+v));
// 按某个条件分组 会得到一个key 为布尔的map
List<Integer> patationList = Arrays.asList(11, 22, 13, 14, 25, 26);
Map<Boolean, List<Integer>> collect3 = patationList.stream()
.collect(Collectors.partitioningBy(age -> age > 18));
// 生成自己的 Stream 流
Random random = new Random();
Stream<Integer> generate = Stream.generate(random::nextInt);
generate.forEach(num -> System.out.println(num));
// 生成自己的 UUID 流
Stream<UUID> generate1 = Stream.generate(UUID::randomUUID);
generate1.limit(5).forEach(System.out::println);

JDK新特性关于流操作部分  

上一篇:JDK 新特性


下一篇:转: 三大WEB服务器对比分析(apache ,lighttpd,nginx) (2008年的旧文,仅供参考之用)