zoukankan      html  css  js  c++  java
  • Java8 Stream

            // 1、排除空串
            List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
            List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
    
            // 2、forEach
    
            filtered.forEach(s -> System.out.println(s)); // ==>filtered.forEach(System.out::println);
    
            // 3、map
    
            List<String> testMap = filtered.stream().map(s -> s + s).collect(Collectors.toList());
    
            // 4、filter
    
            Long count = strings.stream().filter(s -> s.isEmpty()).count();
    
            System.out.println("空串的数量为:count = " + count);
    
            // 5、limit
    
            filtered.stream().limit(2).forEach(System.out::println);
    
    
            // 6、sorted
            System.out.print("6 ==>");
            filtered.stream().sorted(Comparator.comparing(String::toString).reversed()).forEach(System.out::println);
            System.out.print("7 ==>");
    
            // 7、Collectors
    
            String mergeString = filtered.parallelStream().collect(Collectors.joining(","));
    
            System.out.println(mergeString);
    
            //8 、 mapToInt
            OptionalInt max = filtered.stream().mapToInt(s -> Integer.parseInt(s)).max();
  • 相关阅读:
    Ridis学习笔记
    VMware虚拟机安装教程
    Spring中获取Bean的几种方式
    jQuery中的常用事件
    乱码解决
    自动装配
    通配符用法
    Spring
    Spring mvc简单案例
    jdbctemplate
  • 原文地址:https://www.cnblogs.com/hansc-blog/p/10653273.html
Copyright © 2011-2022 走看看