• 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));
        }
    
    }
  • 相关阅读:
    layui多选框
    js获取html5 audio 音频时长方法
    危害程序员职业生涯的三大观念
    选择器
    C++ STL partial_sort
    C++ STL sort
    C++ STL 排列 next_permutation prev_permutation
    C++ STL 逆转旋转 reverse reverse_copy rotate
    C++ unique
    C++ remove remove_if erase
  • 原文地址:https://www.cnblogs.com/bilaisheng/p/10210933.html
Copyright © 2020-2023  润新知