什么是数据流? 集合讲的是数据,而数据流讲的是计算!
List<Person> personList = new ArrayList<Person>(); personList.add(new Person("Tom", 8900, 12, "male", "New York")); personList.add(new Person("Jack", 7000, 13,"male", "Washington")); personList.add(new Person("Lily", 7800, 24,"female", "Washington")); personList.add(new Person("Anni", 8200, 25,"female", "New York")); personList.add(new Person("Owen", 9500, 66,"male", "New York")); personList.add(new Person("Alisa", 7900, 52,"female", "New York")); stream<Person> str = personList.stream()
// foreach personList.stream().filter(x->x.getAge() > 40).forEach(System.out::print); // find (filter、findAny) Optional<Person> optional = personList.stream().filter(f -> f.getAge() == 12).findFirst() Optional<Person> optional = personList.parallelStream().findAny(); // match (返回 true、false) 是否包含 personList.stream().anyMatch(x -> x.getAge() > 52)
// 过滤 personList.stream().filter(x->x.getSalary() ==8900).collect(Collectors.toList()) // Map (取其中一列) personList.stream().map(x->x.getSalary()).collect(Collectors.toList()) // 过滤 + Map personList.stream().filter(f->f.getSalary()==7900).map(x->x.getSalary()).collect(Collectors.toList()) // 映射 案例 (工资整体加1W) personList.stream().map(person -> { person.setSalary(person.getSalary() + 10000); return person; }).collect(Collectors.toList());
// min personList.stream().min(Comparator.comparingInt(Person::getAge)) // max personList.stream().max(Comparator.comparingInt(Person::getAge)) // BigDecimal 求最大值 personList.stream().map(x-x.getSalaryByBigDecimal()).max(BigDecimal::compareTo); // BigDecimal 求最小值 personList.stream().map(x->x.getSalaryByBigDecimal()).min(BigDecimal::compareTo)
主要是针对BigDecimal
// BigDecimal 求和 personList.stream().map(x->x.getSalaryByBigDecimal()).reduce(BigDecimal::add) // BigDecimal 乘法 personList.stream().map(x->x.getSalaryByBigDecimal()).reduce(BigDecimal::multiply) // BigDecimal 平均值 BigDecimal average = userList.stream().map(vo -> ObjectUtils.isEmpty(vo.getWeight()) ? new BigDecimal(0) : vo.getWeight()).reduce(BigDecimal.ZERO, BigDecimal::add).divide(BigDecimal.valueOf(userList.size()), 2, BigDecimal.ROUND_HALF_UP);
collect
,收集,可以说是内容最繁多、功能最丰富的部分了。从字面上去理解,就是把一个流收集起来,最终可以是收集成一个值也可以收集成一个新的集合。
// toList personList.stream().filter(x>x.getAge()==12).collect(Collectors.toList()) // toSet personList.stream().map(x->x.getSex()).collect(Collectors.toSet()) // toMap Map<String, Person> collect = personList.stream().collect(Collectors.toMap(Person::getName, p -> p))
Collectors 工具类 针对Double ,Integer
// 求总数 Long count = personList.stream().collect(Collectors.counting()); // 求平均工资 Double average = personList.stream().collect(Collectors.averagingDouble(Person::getSalary)); // 求最高工资 Optional<Integer> max = personList.stream().map(Person::getSalary).collect(Collectors.maxBy(Integer::compare)); // 求工资之和 Integer sum = personList.stream().collect(Collectors.summingInt(Person::getSalary)); // 一次性统计所有信息 DoubleSummaryStatistics collect = personList.stream().collect(Collectors.summarizingDouble(Person::getSalary));
partitioningBy: 返回值是Map的key是boolean类型,也就是这个函数的返回值只能将数据分为两组也就是ture和false两组数据
groupingBy: 但是他的key是泛型,那么这个分组就会将数据分组成多个key的形式
// 将员工按薪资是否高于8000分组 Map<Boolean, List<Person>> part = personList.stream().collect(Collectors.partitioningBy(x -> x.getSalary() > 8000)); // 将员工按性别分组 Map<String, List<Person>> group = personList.stream().collect(Collectors.groupingBy(Person::getSex)); // 多次分组 先按员工性别分组再按员工地区分组 personList.stream().collect(Collectors.groupingBy(Person::getSex, Collectors.groupingBy(Person::getArea)));
joining
可以将stream中的元素用特定的连接符(没有的话,则直接连接)连接成一个字符串。
personList.stream().map(p -> p.getName()).collect(Collectors.joining(","))
sorted,中间操作。有两种排序:
// 按工资升序排序(自然排序) List<String> newList = personList.stream().sorted(Comparator.comparing(Person::getSalary)).map(Person::getName) .collect(Collectors.toList()); // 按工资倒序排序 List<String> newList2 = personList.stream().sorted(Comparator.comparing(Person::getSalary).reversed()) .map(Person::getName).collect(Collectors.toList()); // 先按工资再按年龄升序排序 List<String> newList3 = personList.stream() .sorted(Comparator.comparing(Person::getSalary).thenComparing(Person::getAge)).map(Person::getName) .collect(Collectors.toList()); Collections.reverse Collections.sort
// 合并 去重 String[] arr1 = { "a", "b", "c", "d" }; String[] arr2 = { "d", "e", "f", "g" }; Stream<String> stream1 = Stream.of(arr1); Stream<String> stream2 = Stream.of(arr2); // concat:合并两个流 distinct:去重 List<String> newList = Stream.concat(stream1, stream2).distinct().collect(Collectors.toList()); // 限制 List<Person> collect = personList.stream().limit(4).collect(Collectors.toList()); // 跳过 long pageNo = 3; long pageSize =2; List<Person> collect = personList.stream().skip((pageNo-1)*pageSize).limit(2).collect(Collectors.toList()); collect.stream().forEach(System.out::println);