0
点赞
收藏
分享

微信扫一扫

Java8之Stream流操作

Sophia的玲珑阁 2022-01-22 阅读 76

Java8之Stream流操作

Stream流操作是Java8的新特性。将要处理的元素集合看作一种流,在流的过程中,借助Stream API对流中的元素进行操作,比如:筛选、排序、聚合等。

一、流的创建

1.通过集合中的stream()方法创建流

// 创建一个Stream流[顺序流]
Stream<String> stream = list.stream();
// 创建一个并行流
Stream<String> parallelStream = list.parallelStream();

2.通过工具类Arrays.stream(T[] array)创建流

// 创建一个String数组
String[] array = new String[]{"kk", "dd", "cc"};
// 通过工具类创建一个数组的流
Stream<String> stream = Arrays.stream(array);

3.通过Stream的静态方法创建流

// Stream.of(T... valus)
Stream<String> stream = Stream.of("aa", "bb", "cc");
// Stream.generate(Supplier<T> s)
// 参数为一个供给型函数式接口[只出不进]
Stream<Double> generate = Stream.generate(Math::random);
// Stream.iterate(final T seed, final UnaryOperator<T> f)
// seed参数作为种子值,类似与传入的初始值
// UnaryOperator函数式接口输入类型与返回类型一致
// 初始值为1, 每进行一次操作将返回一个值为之前的2倍的值
// 【警告】当值超过int最大值后将变为0
Stream<Integer> iterate = Stream.iterate(1, (s) -> s * 2);
// 输出: 1,2,4,8,16...

顺序流是由主线程执行的操作,而并行流内部则以多线程的方式执行操作,对数据的操作没有顺序的情况下可使用并行流进行操作,且当在大数据量的情况下,使用并行流可提升执行效率。

还可通过parallel()方法将顺序流转换为并行流

Stream<String> parallel = list.stream().parallel();

二、流的应用

// 创建集合
List<Integer> list = Arrays.asList(2, 1, 9, 6, 4, 8, 4, 2, 3);
// 

1.元素过滤与查找(filter/find)


// 过滤,筛选符合条件的元素
// 9 6 8
list.stream().filter(e -> e > 5).forEach(System.out::println);
// 查找符合条件的第一个元素
// 9
Optional<Integer> first = list.stream().filter(e -> e > 5).findFirst();
System.out.println(first.get());
// 查找任意符合条件的值
Optional<Integer> any = list.stream().filter(e -> e > 5).findAny();
System.out.println(any.get());

2.聚合(min/max/count)

min获取最小值;

max获取最大值;

count统计个数;

// Stream.max(Comparator<? super T> comparator)
// 传入一个比较器函数式接口对象
// 获取最大值
Integer max = list.stream().max(Comparator.comparing(Integer::intValue)).get();
System.out.println(max);
Optional<Integer> 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<Integer> 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);

3.映射(map/flatMap)

可理解为将流中的元素转换成另一个元素

// 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(BinaryOperator<T> accumulator)
// Stream.reduce(T identity, BinaryOperator<T> 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.收集(collect)

收集,可以说是内容最繁多、功能最丰富的部分了。从字面上去理解,就是把一个流收集起来,最终可以是收集成一个值也可以收集成一个新的集合。

5.1 Collectors.toList()

// 将每个元素值+2 并返回一个新集合
// [4, 3, 11, 8, 6, 10, 6, 4, 5]
List<Integer> intList = list.stream().map(e -> e + 2)
    .collect(Collectors.toList());
System.out.println(intList);

5.2 Collectors.toSet()

// 将每个元素值+2 并返回一个新集合(去重)
// [3, 4, 5, 6, 8, 10, 11]
Set<Integer> inSet = list.stream().map(e -> e + 2)
    .collect(Collectors.toSet());
System.out.println(inSet);

5.3 Collectors.toMap()

// distinct()为去除重复值
// 转换为Map集合
// {1=1, 2=2, 3=3, 4=4, 6=6, 8=8, 9=9}
Map<Integer, Integer> 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()

// 最大值
Optional<Integer> max3 = list.stream().collect(Collectors.maxBy(Integer::compareTo));
System.out.println(max3.get());

5.7 Collectors.minBy()

// 最小值
Optional<Integer> min3 = list.stream().collect(Collectors.minBy(Integer::compareTo));
System.out.println(min3.get());

5.8 Collectors.summingInt()

// 求和
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值,断言型函数式接口
Map<Boolean, List<Integer>> listMap 
	= list.stream().collect(Collectors.partitioningBy(e -> e > 5));
// {false=[2, 1, 4, 4, 2, 3], true=[9, 6, 8]}
System.out.println(listMap);

5.11 Collectors.groupingBy()

// 分组,将数据以数值为key进行分组,相同为一组
Map<Integer, List<Integer>> 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);

5.12 Collectors.joining()

// 拼接字符串
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()

// 规约求和
Optional<Integer> sum4 = list.stream().collect(Collectors.reducing(Integer::sum));
System.out.println(sum4.get());

6.排序(sorted)

// 将数据进行排序
List<Integer> sort 
	= list.stream().sorted(Integer::compareTo).collect(Collectors.toList());
// [1, 2, 2, 3, 4, 4, 6, 8, 9]
System.out.println(sort);

7.去重(distinct)

// 去重集合中重复的元素
List<Integer> list2 = list.stream().distinct().collect(Collectors.toList());
// [2, 1, 9, 6, 4, 8, 3]
System.out.println(list2);

8.跳过(skip)

// 跳过指定位置的元素
// 索引从1开始
List<Integer> list3 = list.stream().skip(1).collect(Collectors.toList());
System.out.println(list3);

9.限制数量(limit)

// 取前四个元素
List<Integer> list4 = list.stream().limit(4).collect(Collectors.toList());
System.out.println(list4);

10.拼接流(concat)

// 将两个流拼接成一个新的流
Stream<Integer> stream = Arrays.stream(new Integer[]{12, 34, 56});
Stream<Integer> concat = Stream.concat(stream, list.stream());
举报

相关推荐

0 条评论