zoukankan      html  css  js  c++  java
  • Java8-Stream-No.05

    
    import java.util.Arrays;
    import java.util.List;
    import java.util.function.Supplier;
    import java.util.stream.Stream;
    
    public class Streams5 {
    
        public static void main(String[] args) {
            List<String> strings =
                Arrays.asList("d2", "a2", "b1", "b3", "c");
    
    //        test1(strings);
    //        test2(strings);
    //        test3(strings);
    //        test4(strings);
    //        test5(strings);
    //        test6(strings);
    //        test7(strings);
            test8(strings);
        }
    
        private static void test8(List<String> stringCollection) {
            Supplier<Stream<String>> streamSupplier =
                () -> stringCollection
                    .stream()
                    .filter(s -> s.startsWith("a"));
    
            streamSupplier.get().anyMatch(s -> true);
            streamSupplier.get().noneMatch(s -> true);
        }
    
        // stream has already been operated upon or closed
        private static void test7(List<String> stringCollection) {
            Stream<String> stream = stringCollection
                .stream()
                .filter(s -> s.startsWith("a"));
    
            stream.anyMatch(s -> true);
            stream.noneMatch(s -> true);
        }
    
        // short-circuit
        private static void test6(List<String> stringCollection) {
            stringCollection
                .stream()
                .map(s -> {
                    System.out.println("map:      " + s);
                    return s.toUpperCase();
                })
                .anyMatch(s -> {
                    System.out.println("anyMatch: " + s);
                    return s.startsWith("A");
                });
        }
    
        private static void test5(List<String> stringCollection) {
            stringCollection
                .stream()
                .filter(s -> {
                    System.out.println("filter:  " + s);
                    return s.toLowerCase().startsWith("a");
                })
                .sorted((s1, s2) -> {
                    System.out.printf("sort:    %s; %s
    ", s1, s2);
                    return s1.compareTo(s2);
                })
                .map(s -> {
                    System.out.println("map:     " + s);
                    return s.toUpperCase();
                })
                .forEach(s -> System.out.println("forEach: " + s));
        }
    
        // sorted = horizontal
        private static void test4(List<String> stringCollection) {
            stringCollection
                .stream()
                .sorted((s1, s2) -> {
                    System.out.printf("sort:    %s; %s
    ", s1, s2);
                    return s1.compareTo(s2);
                })
                .filter(s -> {
                    System.out.println("filter:  " + s);
                    return s.toLowerCase().startsWith("a");
                })
                .map(s -> {
                    System.out.println("map:     " + s);
                    return s.toUpperCase();
                })
                .forEach(s -> System.out.println("forEach: " + s));
        }
    
        private static void test3(List<String> stringCollection) {
            stringCollection
                .stream()
                .filter(s -> {
                    System.out.println("filter:  " + s);
                    return s.startsWith("a");
                })
                .map(s -> {
                    System.out.println("map:     " + s);
                    return s.toUpperCase();
                })
                .forEach(s -> System.out.println("forEach: " + s));
        }
    
        private static void test2(List<String> stringCollection) {
            stringCollection
                .stream()
                .map(s -> {
                    System.out.println("map:     " + s);
                    return s.toUpperCase();
                })
                .filter(s -> {
                    System.out.println("filter:  " + s);
                    return s.startsWith("A");
                })
                .forEach(s -> System.out.println("forEach: " + s));
        }
    
        private static void test1(List<String> stringCollection) {
            stringCollection
                .stream()
                .filter(s -> {
                    System.out.println("filter:  " + s);
                    return true;
                })
                .forEach(s -> System.out.println("forEach: " + s));
        }
    
    }
  • 相关阅读:
    ASP.NET 缓存技术分析
    asp.net中两款文本编辑器NicEdit和Kindeditor
    VS2005,VS2008,VS2010将ASP.NET网站编译成一个DLL文件
    公共的Json操作C#类
    怎么把100多个EXCEL文件合并成一个
    C# ToString()方法一些特殊用法
    C# 将数据导出到Execl汇总
    ASP.NET MVC中在Action获取提交的表单数据方法总结
    vue实现锚点定位跳转(当前页面跳转,url不变)
    es6 去除小数点后,不四舍五入
  • 原文地址:https://www.cnblogs.com/bilaisheng/p/10210933.html
Copyright © 2011-2022 走看看