List<Integer> list = Arrays.asList(7, 6, 9, 3, 8, 2, 1, 10, 11);
list.stream().filter(x -> x > 6).forEach(System.out::println);
//判断成立第一,判断成立随意一位,判断是否存在
Optional<Integer> first = list.stream().filter(x -> x > 6).findFirst();
System.out.println(first.get());
Optional<Integer> any = list.parallelStream().filter(x -> x > 6).findAny();
System.out.println(any.get());
boolean anyMatch = list.stream().anyMatch(x -> x < 6);
System.out.println(anyMatch);
//获取集合中大于7 的数据 并且打印
Stream<Integer> stream = list.stream();
stream.filter(x -> x > 7).forEach(System.out::println);
//获取string集合中 最长的元素
List<String> list2 = Arrays.asList("admin", "123", "12345111111");
Optional<String> max = list2.stream().max(Comparator.comparing(String::length));
System.out.println("获取string集合中 最长的元素" + max.get());
//排序 获取最大值
Optional<Integer> max1 = list.stream().max(Integer::compareTo);
System.out.println("自然排序的最大值" + max1.get());
Optional<Integer> max2 = list.stream().max(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1.compareTo(o2);
}
});
System.out.println("自定义排序最大值" + max2.get());
//获取员工工资最高的人
List<Person> personList = new ArrayList<Person>();
personList.add(new Person("Tom", 9000, 12, "shenzhen"));
personList.add(new Person("jack", 8000, 12, "shenzhen0"));
personList.add(new Person("lily", 2840, 123, "beijing"));
personList.add(new Person("owen", 9500, 44, "beijing"));
Optional<Person> max3 = personList.stream().max(Comparator.comparingInt(Person::getSalary));
System.out.println("员工最高工资" + max3.get());
//计算integer集合大于6的元素个数
long count = list.stream().filter(x -> x > 6).count();
System.out.println("统计数量" + count);
//映射,可以将一个流的元素按照一定的映射规则映射到另一个流中。分为map和flatMap:
//map:接收一个函数作为参数,该函数会被应用到每个元素上,并将其映射成一个新的元素。
//flatMap:接收一个函数作为参数,将流中的每个值都换成另一个流,然后把所有流连接成一个流。
//将英文字符串数组的元素都改为大小写,整数数组元素+3
String[] strArray = {"avsd", "cbd", "sb", "shanpao"};
List<String> strings = Arrays.stream(strArray).map(String::toUpperCase).collect(Collectors.toList());
List<Integer> integers = Arrays.asList(1, 3, 5, 7, 9, 11);
List<Integer> collect = integers.stream().map(x -> x + 3).collect(Collectors.toList());
System.out.println("每个元素大写" + strings);
System.out.println("每个元素+3" + collect);
//员工薪资全部增加1000
List<Person> collect1 = personList.stream().map(person -> {
Person person1 = new Person(person.getName(), 0, 0, null);
person1.setSalary(person.getSalary() + 1000);
return person1;
}).collect(Collectors.toList());
System.out.println("改动前:" + personList.get(0).getName() + "--->" + personList.get(0).getSalary());
System.out.println("改动后:" + collect1.get(0).getName() + "--->" + collect1.get(0).getSalary());
List<Person> collect2 = personList.stream().map(person -> {
person.setSalary(person.getSalary() + 1000);
return person;
}).collect(Collectors.toList());
System.out.println("2改动前:" + personList.get(0).getName() + "--->" + personList.get(0).getSalary());
System.out.println("2改动后:" + collect2.get(0).getName() + "--->" + collect2.get(0).getSalary());
//两个字符数组合并成新的数组
List<String> strings1 = Arrays.asList("m,k,l,v", "1,3,4,5,6");
List<String> collect3 = strings1.stream().flatMap(s -> {
//将每个元素转换成一个stream
String[] split = s.split(",");
Stream<String> s2 = Arrays.stream(split);
return s2;
}).collect(Collectors.toList());
System.out.println("处理前:" + strings1);
System.out.println("处理后:" + collect3);
//归约,也称缩减,顾名思义,是把一个流缩减成一个值,能实现对集合求和、求乘积和求最值操作。
Optional<Integer> sum = list.stream().reduce((x, y) -> x + y);
Optional<Integer> sum2 = list.stream().reduce(Integer::sum);
Integer sum3 = list.stream().reduce(0, Integer::sum);
//乘法
Optional<Integer> product = list.stream().reduce((x, y) -> x * y);
//最大值
Optional<Integer> maxmax = list.stream().reduce((x, y) -> x > y ? x : y);
Integer maxmax2 = list.stream().reduce(1, Integer::max);
System.out.println("求和" + sum.get() + "," + sum2.get() + "," + sum3);
System.out.println("2乘法" + product.get());
System.out.println("最大值" + maxmax.get() + "," + maxmax2);
//员工的工资之和 跟 最高工资
Optional<Integer> sumSalary = personList.stream().map(Person::getSalary).reduce(Integer::sum);
//最高工资
Integer maxSalary = personList.stream().map(Person::getSalary).reduce(1, Integer::max);
System.out.println("工资总计" + sumSalary.get());
System.out.println("最高工资" + maxSalary);
// 归集(toList/toSet/toMap)
//因为流不存储数据,那么在流中的数据完成处理后,需要将流中的数据重新归集到新的集合里。
// toList、toSet和toMap比较常用,另外还有toCollection、toConcurrentMap等复杂一些的用法。
List<Integer> collect4 = list.stream().filter(x -> x % 2 == 0).collect(Collectors.toList());
Set<Integer> collect5 = list.stream().filter(x -> x % 2 == 0).collect(Collectors.toSet());
Map<String, Person> collect6 = personList.stream().filter(p -> p.getSalary() > 8000).collect(Collectors.toMap(Person::getName, p -> p));
System.out.println("toList" + collect4);
System.out.println("toSet" + collect5);
System.out.println("toMap" + collect6);
//统计计数:count
//平均值:averagingInt、averagingLong、averagingDouble
//最值:maxBy、minBy
//求和:summingInt、summingLong、summingDouble
//统计以上所有:summarizingInt、summarizingLong、summarizingDouble
//统计员工总人数、平均工资、工资总额、最高工资。
Long collect7 = personList.stream().collect(Collectors.counting());
Double collect8 = personList.stream().collect(Collectors.averagingDouble(Person::getSalary));
Integer collect10 = personList.stream().collect(Collectors.summingInt(Person::getSalary));
Optional<Integer> collect9 = personList.stream().map(Person::getSalary).collect(Collectors.maxBy(Integer::compareTo));
//一次性统计所有信息
DoubleSummaryStatistics collect11 = personList.stream().collect(Collectors.summarizingDouble(Person::getSalary));
System.out.println("员工总数:" + collect7);
System.out.println("员工平均工资:" + collect8);
System.out.println("员工工资总和:" + collect10);
System.out.println("员工最高工资:" + collect9);
System.out.println("员工工资所有统计:" + collect11);
//分组分组(partitioningBy/groupingBy)
//分区:将stream按条件分为两个Map,比如员工按薪资是否高于8000分为两部分。
//分组:将集合分为多个Map,比如员工按性别分组。有单级分组和多级分组。
//员工薪资高于8000分为两部分,员工按年龄跟地区分组
Map<Boolean, List<Person>> part = personList.stream().collect(Collectors.partitioningBy(x -> x.getSalary() > 8000));
//将员工按照年龄分组
Map<Integer, List<Person>> group = personList.stream().collect(Collectors.groupingBy(Person::getAge));
//先年龄 后地区分组
Map<Integer, Map<String, List<Person>>> group2 = personList.stream().collect(Collectors.groupingBy(Person::getAge, Collectors.groupingBy(Person::getArea)));
System.out.println("员工按薪资是否大于8000分组情况:" + part);
System.out.println("将员工按照年龄分组:" + group);
System.out.println("先年龄 后地区分组:" + group2);
//接合(joining)
//joining可以将stream中的元素用特定的连接符(没有的话,则直接连接)连接成一个字符串。
String collect12 = personList.stream().map(p -> p.getName()).collect(Collectors.joining(","));
System.out.println("所有员工得姓名:" + collect12);
List<String> strings2 = Arrays.asList("A", "B", "C");
String collect13 = strings2.stream().collect(Collectors.joining("-"));
System.out.println("拼接后得字符串:" + collect13);
// 归约(reducing)
//Collectors类提供的reducing方法,相比于stream本身的reduce方法,增加了对自定义归约的支持。
// 每个员工减去起征点后的薪资之和(这个例子并不严谨,但一时没想到好的例子)
Integer sum11 = personList.stream().collect(Collectors.reducing(0, Person::getSalary, (i, j) -> (i + j - 5000)));
System.out.println("员工扣税薪资总和:" + sum11);
// stream的reduce
Optional<Integer> sum22 = personList.stream().map(Person::getSalary).reduce(Integer::sum);
System.out.println("员工薪资总和:" + sum22.get());
//排序(sorted)
//sorted,中间操作。有两种排序:
//sorted():自然排序,流中元素需实现Comparable接口
//sorted(Comparator com):Comparator排序器自定义排序
//案例:将员工按工资由高到低(工资一样则按年龄由大到小)排序
//按工资升序排序(自然排序)
List<String> collect14 = personList.stream().sorted(Comparator.comparing(Person::getSalary)).map(Person::getName).collect(Collectors.toList());
//按工资倒序排序
List<String> collect15 = personList.stream().sorted(Comparator.comparing(Person::getSalary).reversed()).map(Person::getName).collect(Collectors.toList());
//先按工资 再按年龄升序
List<String> collect16 = personList.stream().sorted(Comparator.comparing(Person::getSalary).thenComparing(Person::getAge)).map(Person::getName).collect(Collectors.toList());
//先按工资,再按年龄自定义排序(降序)
List<String> collect17 = personList.stream().sorted((p1, p2) -> {
if (p1.getSalary() == p2.getSalary()) {
return p2.getAge() - p1.getAge();
} else {
return p2.getSalary() - p1.getSalary();
}
}).map(Person::getName).collect(Collectors.toList());
System.out.println("按工资升序排序:" + collect14);
System.out.println("按工资降序排序:" + collect15);
System.out.println("先按工资再按年龄升序排序:" + collect16);
System.out.println("先按工资再按年龄自定义降序排序:" + collect17);
//提取/组合
//流也可以进行合并、去重、限制、跳过等操作。
String[] result1 = {"a","b","c","d"};
String[] result2 = {"e","f","g","d"};
Stream<String> result11 = Stream.of(result1);
Stream<String> result22 = Stream.of(result2);
//concat: 合并两个流 distinct:去重
List<String> collect18 = Stream.concat(result11, result22).distinct().collect(Collectors.toList());
//limit : 限制从流中取前n个数据
List<Integer> collect19 = Stream.iterate(1, x -> x+2).limit(10).collect(Collectors.toList());
//skip 跳过前n个数据
List<Integer> collect20 = Stream.iterate(1, x -> x + 2).skip(1).limit(5).collect(Collectors.toList());
System.out.println("流合并:" + collect18);
System.out.println("limit:" + collect19);
System.out.println("skip:" + collect20);
【练习stream常用基本操作】stream语法糖手敲练习。
最新推荐文章于 2024-09-15 14:38:09 发布
本文详细介绍了如何使用Java 8 Stream API对整数列表、字符串、Person对象进行各种操作,如过滤、查找、排序、映射、归约、分组等,涵盖了核心功能和高级技巧。
561





