Stream流式编程
1.流式编程
Stream就是【流】
的意思,与 java.io包中的输入流,输出流是两个不同的概念。Stream的操作是在内存中,主要实现对数据的计算,过滤操作
2.操作分类
创建流:可以通过集合、数组、IO资源、Stream的构造函数创建
• 中间操作:对数据的计算操作,比如筛选,去重,转换等操作,一个中间操作返回一个新的Stream,来支持连续计算
• 终止操作:每个流只能有一次终止操作,终止之后流无法使用,会产生一个计算结果,可以根据需求转换为想要的结果类型
3.举例
3.1获取运费大于5000的订单并且放到新集合中
// 1) 通过集合的stream()方法创建流对象
Stream<Waybill> stream = waybills.stream();
// 2) 通过流对象的方法计算数据,filter:过滤数据
// filter接收一个过滤条件,item为当前操作的元素,比较价格是否大于5000,满足条件的过滤出来,放到一个新的Stream对象中
Stream<Waybill> waybillStream = stream.filter(item -> item.getPrice().compareTo(new BigDecimal(5000)) == 1);
// 3) 将过滤后的stream转换为新的集合,调用collect方法即可,toList()转换为List集合,toSet转换为Set集合
List<Waybill> collect = waybillStream.collect(Collectors.toList());
// 遍历,通过方法引用遍历
collect.forEach(System.out::println);
3.2按照运费从高到低排序
List<Waybill> collect1 = waybills.stream().sorted(Comparator.comparing(Waybill::getPrice)).collect(Collectors.toList());
System.out.println("**升序排序**");
collect1.forEach(System.out::println);
// 2、降序排序,调用reversed方法即可降序
List<Waybill> collect2 = waybills.stream().sorted(Comparator.comparing(Waybill::getPrice).reversed()).collect(Collectors.toList());
System.out.println("**降序排序**");
collect2.forEach(System.out::println);
// 3、如果只想获取运单号
List<String> collect3 = waybills.stream().sorted(Comparator.comparing(Waybill::getPrice)).map(Waybill::getWayNo).collect(Collectors.toList());
System.out.println("**降序只获取运单号**");
collect3.forEach(System.out::println);
// 4、先按距离,再按运费,通过thenComparing方法做继续排序
List<Waybill> collect4 = waybills.stream().sorted(Comparator.comparing(Waybill::getDistance).thenComparing(Waybill::getPrice)).collect(Collectors.toList());
System.out.println("**先按距离再按运费**");
collect4.forEach(System.out::println);
// 5、自定义排序
List<Waybill> collect5 = waybills.stream().sorted((o1, o2) -> {
// 排序规则:根据货物类型排序,相同的根据距离排序
if (o1.getFreightType().equals(o2.getFreightType())) {
return o1.getDistance().compareTo(o2.getDistance());
} else {
return o1.getFreightType().compareTo(o2.getFreightType());
}
}).collect(Collectors.toList());
3.3统计最高最低平均运费
// 1、最高运费,通过max方法
Optional<Waybill> max = waybills.stream().max(Comparator.comparing(Waybill::getPrice));
System.out.println("**运费最高**");
System.out.println(max);
// 2、最低运费,通过min方法
Optional<Waybill> min = waybills.stream().min(Comparator.comparing(Waybill::getPrice));
System.out.println("**运费最低**");
System.out.println(min);
// 3、平均运费,通过 Collectors.averagingDouble计算平均值,需要将 BigDecimal转换为double类型
Double avg = waybills.stream().collect(Collectors.averagingDouble(item -> item.getPrice().doubleValue()));
System.out.println("**平均运费**");
System.out.println(avg);
3.4将运单按货物类型分类,将运单按货物类型和目的地分类,将运单按照运费是否高于5000元分为两部分
// 1、运单按货物类型分类
Map<String, List<Waybill>> collect1 = waybills.stream().collect(Collectors.groupingBy(Waybill::getFreightType));
System.out.println("** 运单按货物类型分类 **");
collect1.forEach((key,value) -> {
System.out.println("key==>" + key + ",value==>" + value);
});
// 2、运单按货物类型和目的地分类
Map<String, Map<String, List<Waybill>>> collect2 = waybills.stream().collect(Collectors.groupingBy(Waybill::getFreightType, Collectors.groupingBy(Waybill::getEndAddress)));
System.out.println("** 运单按货物类型和目的地分类 **");
collect2.forEach((key,value) -> {
System.out.println("key==>" + key + ",value==>" + value);
});
// 3、运单按照运费是否高于5000元分为两部分,这个叫分区了
Map<Boolean, List<Waybill>> collect3 = waybills.stream().collect(Collectors.partitioningBy(item -> item.getPrice().compareTo(new BigDecimal(5000)) == 1));
System.out.println("** 运单按照5000分区 **");
collect3.forEach((key,value) -> {
System.out.println("key==>" + key + ",value==>" + value);
});