Stream流操作是Java8的新特性。将要处理的元素集合看作一种流,在流的过程中,借助Stream API对流中的元素进行操作,比如:筛选、排序、聚合等。
一、流的创建1.通过集合中的stream()方法创建流
// 创建一个Stream流[顺序流] Streamstream = list.stream(); // 创建一个并行流 Stream parallelStream = list.parallelStream();
2.通过工具类Arrays.stream(T[] array)创建流
// 创建一个String数组
String[] array = new String[]{"kk", "dd", "cc"};
// 通过工具类创建一个数组的流
Stream stream = Arrays.stream(array);
3.通过Stream的静态方法创建流
// Stream.of(T... valus) Streamstream = Stream.of("aa", "bb", "cc"); // Stream.generate(Supplier s) // 参数为一个供给型函数式接口[只出不进] Stream generate = Stream.generate(Math::random); // Stream.iterate(final T seed, final UnaryOperator f) // seed参数作为种子值,类似与传入的初始值 // UnaryOperator函数式接口输入类型与返回类型一致 // 初始值为1, 每进行一次操作将返回一个值为之前的2倍的值 // 【警告】当值超过int最大值后将变为0 Stream iterate = Stream.iterate(1, (s) -> s * 2); // 输出: 1,2,4,8,16...
顺序流是由主线程执行的操作,而并行流内部则以多线程的方式执行操作,对数据的操作没有顺序的情况下可使用并行流进行操作,且当在大数据量的情况下,使用并行流可提升执行效率。
还可通过parallel()方法将顺序流转换为并行流
Stream二、流的应用parallel = list.stream().parallel();
// 创建集合 List1.元素过滤与查找(filter/find)list = Arrays.asList(2, 1, 9, 6, 4, 8, 4, 2, 3); //
// 过滤,筛选符合条件的元素 // 9 6 8 list.stream().filter(e -> e > 5).forEach(System.out::println); // 查找符合条件的第一个元素 // 9 Optional2.聚合(min/max/count)first = list.stream().filter(e -> e > 5).findFirst(); System.out.println(first.get()); // 查找任意符合条件的值 Optional any = list.stream().filter(e -> e > 5).findAny(); System.out.println(any.get());
min获取最小值;
max获取最大值;
count统计个数;
// Stream.max(Comparator super T> comparator) // 传入一个比较器函数式接口对象 // 获取最大值 Integer max = list.stream().max(Comparator.comparing(Integer::intValue)).get(); System.out.println(max); Optional3.映射(map/flatMap)max1 = list.stream().max(Integer::compareTo); System.out.println(max1.get()); // 获取最小值 Integer min = list.stream().min(Comparator.comparing(Integer::intValue)).get(); System.out.println(min); Optional min1 = list.stream().min(Integer::compareTo); System.out.println(min1.get()); // 统计元素大于5的个数 long count = list.stream().filter(e -> e > 5).count(); System.out.println(count);
可理解为将流中的元素转换成另一个元素
// map使用
// 将元素的每个值乘以2
list.stream().map(e -> e * 2).forEach(System.out::println);
// flagtMap使用
// 将一个元素转换成一个流返回
list.stream().flatMap(e -> Arrays.stream(new Integer[]{e}))
.forEach(System.out::println);
4.规约(reduce)
归约,也称缩减,顾名思义,是把一个流缩减成一个值,能实现对集合求和、求乘积和求最值操作。
与聚合函数作用类似,将多值变为一个值例如求和操作
// Stream.reduce(BinaryOperator5.收集(collect)accumulator) // Stream.reduce(T identity, BinaryOperator accumulator) // 求和操作 Integer sum = list.stream().reduce((x, y) -> x + y).get(); System.out.println(sum); Integer sum1 = list.stream().reduce(Integer::sum).get(); System.out.println(sum1); Integer sum2 = list.stream().reduce(0, Integer::sum); System.out.println(sum2); // 减法操作,由第一个元素减去后面所有元素 Integer subtract = list.stream().reduce((x, y) -> x - y).get(); System.out.println(subtract); // 最大值 Integer max2 = list.stream().reduce(Integer::max).get(); System.out.println(max2); // 最小值 Integer min2 = list.stream().reduce(Integer::min).get(); System.out.println(min2);
收集,可以说是内容最繁多、功能最丰富的部分了。从字面上去理解,就是把一个流收集起来,最终可以是收集成一个值也可以收集成一个新的集合。
5.1 Collectors.toList()// 将每个元素值+2 并返回一个新集合 // [4, 3, 11, 8, 6, 10, 6, 4, 5] List5.2 Collectors.toSet()intList = list.stream().map(e -> e + 2) .collect(Collectors.toList()); System.out.println(intList);
// 将每个元素值+2 并返回一个新集合(去重) // [3, 4, 5, 6, 8, 10, 11] Set5.3 Collectors.toMap()inSet = list.stream().map(e -> e + 2) .collect(Collectors.toSet()); System.out.println(inSet);
// distinct()为去除重复值
// 转换为Map集合
// {1=1, 2=2, 3=3, 4=4, 6=6, 8=8, 9=9}
Map intMap = list.stream()
.distinct()
.collect(Collectors.toMap(Integer::valueOf, Integer::intValue));
System.out.println(intMap);
5.4 Collectors.counting()
// 统计值 Long counting = list.stream().collect(Collectors.counting()); System.out.println(counting);5.5 Collectors.averagingInt()
// 求平均值 Double avg = list.stream().collect(Collectors.averagingInt(Integer::intValue)); System.out.println(avg);5.6 Collectors.maxBy()
// 最大值 Optional5.7 Collectors.minBy()max3 = list.stream().collect(Collectors.maxBy(Integer::compareTo)); System.out.println(max3.get());
// 最小值 Optional5.8 Collectors.summingInt()min3 = list.stream().collect(Collectors.minBy(Integer::compareTo)); System.out.println(min3.get());
// 求和 Integer summing = list.stream().collect(Collectors.summingInt(Integer::intValue)); System.out.println(summing);5.9 Collectors.summarizingInt()
// 统计以上所有
IntSummaryStatistics collect
= list.stream().collect(Collectors.summarizingInt(Integer::intValue));
// IntSummaryStatistics{count=9, sum=39, min=1, average=4.333333, max=9}
System.out.println(collect);
5.10 Collectors.partitioningBy()
// 分组,将数据分为两组 // Collectors.partitioningBy(Predicate super T> predicate) // 传入一个参数,返回一个boolean值,断言型函数式接口 Map5.11 Collectors.groupingBy()> listMap = list.stream().collect(Collectors.partitioningBy(e -> e > 5)); // {false=[2, 1, 4, 4, 2, 3], true=[9, 6, 8]} System.out.println(listMap);
// 分组,将数据以数值为key进行分组,相同为一组 Map5.12 Collectors.joining()> listMap1 = list.stream().collect(Collectors.groupingBy(Integer::intValue)); // {1=[1], 2=[2, 2], 3=[3], 4=[4, 4], 6=[6], 8=[8], 9=[9]} System.out.println(listMap1);
// 拼接字符串
String join = list.stream().map(String::valueOf).collect(Collectors.joining(","));
// 2,1,9,6,4,8,4,2,3
System.out.println(join);
5.13 Collectors.reducing()
// 规约求和 Optional6.排序(sorted)sum4 = list.stream().collect(Collectors.reducing(Integer::sum)); System.out.println(sum4.get());
// 将数据进行排序 List7.去重(distinct)sort = list.stream().sorted(Integer::compareTo).collect(Collectors.toList()); // [1, 2, 2, 3, 4, 4, 6, 8, 9] System.out.println(sort);
// 去重集合中重复的元素 List8.跳过(skip)list2 = list.stream().distinct().collect(Collectors.toList()); // [2, 1, 9, 6, 4, 8, 3] System.out.println(list2);
// 跳过指定位置的元素 // 索引从1开始 List9.限制数量(limit)list3 = list.stream().skip(1).collect(Collectors.toList()); System.out.println(list3);
// 取前四个元素 List10.拼接流(concat)list4 = list.stream().limit(4).collect(Collectors.toList()); System.out.println(list4);
// 将两个流拼接成一个新的流 Streamstream = Arrays.stream(new Integer[]{12, 34, 56}); Stream concat = Stream.concat(stream, list.stream());



