• java8 Stream api详解


    Stream3个阶段

    1. 创建Stream
    2. Stream中间处理
    3. 终止Steam

    img

    创建Stream方式

            List<String> list = new ArrayList<>();
            list.stream();
            Stream.of(list);
            list.parallelStream();
            Stream.builder();
            //generate返回无限连续流,为了限制流中元素的数量,我们可以使用Stream.limit方法
            Stream.generate();
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    函数式基础接口

    接口名入参返回描述
    FunctionTR接受一个泛型 T 对象,返回一个泛型 R 对象,即参数类型和返回类型可以不同
    ConsumerT接受一个泛型 类型参数,进行处理后无任何返回值,常用于消费数据
    PredicateTboolean接受一个泛型 参数,返回值为布尔类型**。Predicate 常用于数据过滤,如过滤出集合中符合某个条件的元素
    SupplierT由于没有参数输入,所以多用于对象创建,类似于一个对象创建工厂。可以使用 Lambda 方式创建任意对象,也可以使用对象构造方法的方法引用创对象
    OperatorTTFunction 函数接口的扩展

    Stream中间处理

    函数名入参返回描述
    filterPredicateStream按照条件过滤符合要求的元素, 返回新的stream流
    mapFunctionStream将已有元素转换为另一个对象类型,一对一逻辑,返回新的stream流
    mapToIntToIntFunctionIntStream将已有元素转换为另一个int对象类型,一对一逻辑,返回新的stream流
    mapToLongToLongFunctionLongStream将已有元素转换为另一个Long对象类型,一对一逻辑,返回新的stream流
    mapToDoubleToDoubleFunctionDoubleStream将已有元素转换为另一个Double对象类型,一对一逻辑,返回新的stream流
    flatMapFunction>Stream将已有元素转换为另一个int对象类型,一对多逻辑,即原来一个元素对象可能会转换为1个或者多个新类型的元素,返回新的stream流
    flatMapToIntFunctionIntStream将已有元素转换为另一个对象类型,一对多逻辑,即原来一个元素对象可能会转换为1个或者多个新类型的元素,返回新的stream流
    flatMapToLongFunctionLongStream将已有元素转换为另一个Long对象类型,一对多逻辑,即原来一个元素对象可能会转换为1个或者多个新类型的元素,返回新的stream流
    flatMapToDoubleFunctionDoubleStream将已有元素转换为另一个Double对象类型,一对多逻辑,即原来一个元素对象可能会转换为1个或者多个新类型的元素,返回新的stream流
    distinctStream对Stream中所有元素进行去重,返回新的stream流(并行流效率低)
    sortedStream根据自然顺序进行排序
    sortedComparator comparatorStream对stream中所有的元素按照指定规则进行排序,返回新的stream流
    peekConsumerStream此方法主要用于支持调试,您希望在元素流过管道中的某个点时看到这些元素,返回新的stream流
    limitlongStream仅保留集合前面指定个数的元素,返回新的stream流(并行流效率低)
    skiplongStream跳过集合前面指定个数的元素,返回新的stream流(并行流效率低)

    终止Steam

    函数名入参返回描述
    forEachConsumer无返回值,对元素进行逐个遍历,然后执行给定的处理逻辑
    forEachOrdered无返回值,始终遵循顺序,对元素进行逐个遍历,然后执行给定的处理逻辑
    toArrayObject[]将流转换为数组
    toArrayIntFunction A[]将流转换为指定类型数组
    reduceT,BinaryOperatorT归约,有默认值或初始值,返回单一结果
    reduceBinaryOperatorOptional归约, 返回单一结果Optional
    reduceU,BiFunction,BinaryOperatorU归约,有默认值或初始值,返回单一结果(并行流时第三参数生效)
    collectCollectorCollector将流转换为指定的类型,通过Collectors进行指定
    collectSupplier,BiConsumer,BiConsumerSupplier将流转换为指定的类型,自定义Collector参数
    minComparatorT返回stream处理后的元素最小值
    maxComparatorT返回stream处理后的元素最大值
    countlong返回stream处理后最终的元素个数
    anyMatchPredicateboolean返回一个boolean值,类似于isContains(),用于判断是否有符合条件的元素
    allMatchPredicateboolean回一个boolean值,用于判断是否所有元素都符合条件
    noneMatchPredicateboolean返回一个boolean值, 用于判断是否所有元素都不符合条件
    findFirstT找到第一个符合条件的元素时则终止流处理
    findAnyT找到任何一个符合条件的元素时则退出流处理,这个对于串行流时与findFirst相同,对于并行流时比较高效,任何分片中找到都会终止后续计算逻辑

    案例

    public class XStream {
    
        public static void main(String[] args) {
            List<String> a = new ArrayList<String>();
            a.add("1");
            a.add("2");
            a.add("2");
            a.add("4");
            a.add("3");
    
            List<String> b = new ArrayList<String>();
            b.add("a");
            b.add("b");
            b.add("c");
            b.add("d");
    
    
            List<User> users = new ArrayList<>();
    
            User user1=new User("马超",18);
            User user2=new User("小乔",16);
            User user3=new User("典韦",27);
            User user4=new User("诸葛亮",24);
            User user5=new User("刘备",23);
            users.add(user1);
            users.add(user2);
            users.add(user3);
            users.add(user4);
            users.add(user5);
    
    
            new XStream().maxUser(users);
        }
    
        public void create(){
            List<String> list = new ArrayList<String>();
            list.add("1");
            list.add("2");
            list.add("3");
            list.add("4");
            Stream<String> stream = list.stream();
            Stream<List<String>> list1 = Stream.of(list);
            Stream<String> stringStream = list.parallelStream();
            Stream.Builder<String> builder = Stream.builder();
            builder.add("1");
            builder.add("2");
            builder.add("3");
            builder.add("4");
            Stream<String> build = builder.build();
            Supplier<String> supplier=()->"aaaa";
            Stream<String> generate = Stream.generate(supplier).limit(3);
            generate.forEach(System.out::println);
        }
    
        public void filter(List<String> list){
            Predicate<String> predicate=a->!a.equals(2);
            Stream<String> stream = list.stream().filter(predicate);
        }
    
        public void map(List<String> list){
            Function<String,Integer> function=x->Integer.valueOf(x);
            Stream<Integer> stream = list.stream().map(function);
        }
    
        public void mapToInt(List<String> list){
            ToIntFunction<String> function= x->Integer.valueOf(x);
            IntStream intStream = list.stream().mapToInt(function);
        }
    
        public void flatMap(List<String> a,List<String> b){
            List<List<String>> ab=new ArrayList<>();
            ab.add(a);
            ab.add(b);
            List<String> collect = ab.stream().flatMap(Collection::stream).collect(Collectors.toList());
            collect.forEach(System.out::println);
        }
    
        public void flatMapToInt(List<String> a,List<String> b){
            List<List<String>> ab=new ArrayList<>();
            ab.add(a);
            ab.add(b);
            ArrayList<Integer> collect = ab.stream().flatMapToInt(i -> i.stream().mapToInt(Integer::valueOf)).collect(ArrayList::new, List::add, List::addAll);
            collect.forEach(System.out::println);
    
        }
        public void distinct(List<String> a){
            List<String> collect = a.stream().distinct().collect(Collectors.toList());
            collect.forEach(System.out::println);
        }
    
        public void sorted(List<String> a){
            List<String> collect = a.stream().sorted().collect(Collectors.toList());
            collect.forEach(System.out::println);
        }
        public void sorted2(List<String> a){
            List<String> collect = a.stream().sorted(Comparator.comparing(x->!x.equals("2"))).collect(Collectors.toList());
            collect.forEach(System.out::println);
        }
        public void peek(List<String> a){
            List<String> collect = a.stream().peek(System.out::println).sorted(Comparator.comparing(x->!x.equals("2"))).peek(System.out::println).collect(Collectors.toList());
            collect.forEach(System.out::println);
        }
        public void limit(List<String> a){
            List<String> collect = a.stream().limit(2).collect(Collectors.toList());
            collect.forEach(System.out::println);
        }
        public void skip(List<String> a){
            List<String> collect = a.stream().skip(2).collect(Collectors.toList());
            collect.forEach(System.out::println);
        }
    
    
    
        public void forEach(List<String> a){
            a.stream().forEach(System.out::println);
        }
    
        public void forEachOrdered(List<String> a){
            a.parallelStream().forEachOrdered(System.out::println);
            System.out.println("-------------");
            a.parallelStream().forEach(System.out::println);
        }
    
        public void toArray(List<String> a){
            Object[] objects = a.stream().toArray();
            System.out.println(objects);
        }
    
        public void toArray2(List<String> a){
            String[] oarrs = a.stream().toArray(String[]::new);
            System.out.println(oarrs);
        }
    
        public void reduce(List<String> a){
            Optional<String> reduce = a.stream().reduce((x, y) -> x.concat(y));
            System.out.println(reduce.get());
        }
    
        public void reduce2(List<String> a){
            String reduce = a.stream().reduce("a",(x, y) -> x.concat(y));
            System.out.println(reduce);
        }
    
        public void reduce3(List<String> a){
            String reduce1 = a.stream().reduce("a",(x, y) -> x.concat(y),(x, y) -> x.concat(y));
            System.out.println(reduce1);
            String reduce2 = a.parallelStream().reduce("a",(x, y) -> x.concat(y),(x, y) -> x.concat(y));
            System.out.println(reduce2);
        }
    
        public void collect(List<String> a){
            List<String> collect = a.stream().collect(Collectors.toList());
        }
    
        public void collect2(List<String> a){
            BiConsumer<List<String>, String> accumulator=(x,y)->{
                if(y.equals("2")){
                    x.add(y);
                }
            };
            BiConsumer<List<String>, List<String>> combiner=(x,y)->{
                List<String> collect = y.stream().map(m -> "a" + m).collect(Collectors.toList());
                x.addAll(collect);
            };
            List<String> collect = a.parallelStream().collect(ArrayList::new,accumulator,combiner);
            collect.stream().forEach(System.out::println);
        }
    
    
        public void min(List<String> a){
            List<Integer> collect = a.stream().mapToInt(Integer::valueOf).collect(ArrayList::new,ArrayList::add,ArrayList::addAll);
            Optional<Integer> min = collect.stream().min(Comparator.comparing(x -> x));
            System.out.println(min.get());
        }
    
        public void max(List<String> a){
            List<Integer> collect = a.stream().mapToInt(Integer::valueOf).collect(ArrayList::new,ArrayList::add,ArrayList::addAll);
            Optional<Integer> max = collect.stream().max(Comparator.naturalOrder());
            System.out.println(max.get());
        }
    
        public void maxUser(List<User> a){
            Optional<User> max = a.stream().max(Comparator.comparing(x -> x.getAge()));
            System.out.println(max.get());
        }
    
        public void count(List<String> a){
            List<Integer> collect = a.stream().mapToInt(Integer::valueOf).collect(ArrayList::new,ArrayList::add,ArrayList::addAll);
            long count = collect.stream().count();
            System.out.println(count);
        }
    
        public void anyMatch(List<String> a){
            boolean b = a.stream().anyMatch(m -> m.equals("2"));
            System.out.println(b);
        }
    
        public void allMatch(List<String> a){
            boolean b = a.stream().allMatch(m -> !m.equals("3"));
            System.out.println(b);
        }
    
        public void noneMatch(List<String> a){
            boolean b = a.stream().noneMatch(m -> m.equals("7"));
            System.out.println(b);
        }
    
        public void findFirst(List<String> a){
            Optional<String> first = a.stream().findFirst();
            System.out.println(first.get());
        }
    
        public void findAny(List<String> a){
            Optional<String> first = a.stream().findAny();
            System.out.println(first.get());
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
  • 相关阅读:
    超全详细,解决校园网没有路由器实现开发板和虚拟机直连
    Dubbo框架基本使用
    wget同时下载多个文件
    解析几何@平面上点到直线的距离@点到平面的距离@空间中点到直线的距离
    淘宝/天猫API:item_search_shop-获得店铺的所有商品
    在PCB走线中线不直,有小疙瘩,让走线变直的方法
    PowerBI中导出数据方法汇总
    Linux驱动开发(十三)---USB驱动HID开发学习(鼠标)
    QT笔记——QT组合键 成为 快捷键
    C++第二十二弹---vector深度剖析及模拟实现(下)
  • 原文地址:https://blog.csdn.net/qq_41639347/article/details/126102920