jdk8 Stream流式处理

1、Strem函数

List<People> list = new ArrayList<>();
//根据age对list分组,得到map
Map<String,List<People>> groupByAge=list.stream().collect(Collectors.groupingBy(People::getAge));
//进阶通过partitioningBy进行分区(分组),返回值的键仍然是布尔类型,但是它的分类是根据范围进行分类的,分区比较适合处理根据范围进行分类
Map<Boolean,List<People>> rs=list.stream().collect(partitioningBy(item->item.getAge<30));


//根据age进行排序(reserve倒序)得到排序后的List
List<People> peopleListSorted = list.stream().sorted(Comparator.comparing(People::getAge).reversed()).collect(Collectors.toList());
//提取age,去重后排序
List<String> ageList = list.stream().map(People::getAge).distinct().sorted().collect(Collectors.toList());
//提取年龄大于20的people
List<People> olderThan20 = list.stream().filter(e->Integer.parseInt(e.getAge()) > 20).collect(Collectors.toList());
//累加List中的money
BigDecimal totalMoney = list.stream().map(People::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);

Integer sum=list.stream().map(People::getMoney).reduce((x,y)->x+y).get();
Double sum = list.stream().map(People::getMoney).reduce(0.0,Double::sum);

//查询
People people = list.stream().filter(e->e.getAge().equals("20")).findFirst().orElse(null);
//List<People> -> Map<String,People> (name,people)
//对象集合转化为map
Map<String,People> map = list.stream().collect(Collectors.toMap(People::getName,n->n,(key1,key2)->key1));
//查找流中最大值和最小值
List<Person> personList = generatePersonList();
Person olderOne = personList.stream().max(Comparator.comparing(Person::getAge)).orElse(null);
Person youngerOne = personList.stream().min(Comparator.comparing(Person::getAge)).orElse(null);

//对象去重
ArrayList<Person> collect = personList.stream().collect(
Collectors.collectingAndThen(
Collectors.toCollection
(() -> new TreeSet<>(Comparator.comparingInt(Person::getAge))),ArrayList::new)
);
//通过joining拼接流中的元素
String result=list.stream().map(People::getName).collect(Collectors.joining(", "));
//skip跳过流中前2个元素
Stream<People> rs = list.stream().skip(2);

2、optinal判断空处理

对于深度嵌套的语句,可能需要多次判空,才能保证代码的健壮性,但是用if来实现,会有一堆的if语句,java8通过optinal比较优雅的解决了这个问题。
举个例子:
String isocode = user.getAddress().getCountry().getIsocode().toUpperCase();

通常if判断的做法
if (user != null) {
    Address address = user.getAddress();
    if (address != null) {
        Country country = address.getCountry();
        if (country != null) {
            String isocode = country.getIsocode();
            if (isocode != null) {
                isocode = isocode.toUpperCase();
            }
        }
    }
}
Optional的做法
String isocode = Optional.ofNullable(user)
  .map(User::getAddress)
  .map(Address::getCountry)
  .map(Country::getIsocode)
  .orElse("default");

3、Stream的groupby

group by生成一个拥有分组功能的Collector,有三个重载方法

  • 需要一个参数:按照该参数进行分组。结果返回一个Map集合,每个Map的key默认是分组参数的类型,value是一个List集合。
Map <String,List < User >> collect = 
users.stream().collect(Collectors.groupingBy(User: :getEdu));
  • 需要两个参数:第二参数是Collector类型,可以对value进行处理。
可以对结果进行映射
Map <String,List <Integer>> collect = 
users.stream().collect(
Collectors.groupingBy(User: :getEdu,
//第二个参数对Map的value进行处理(映射)
Collectors.mapping(User: :getId, Collectors.toList()))
);



可以对结果进行求和
Map <String,Double> collect = users.stream().collect(
Collectors.groupingBy(User: :getEdu,
    //对参数进行累计求和
Collectors.summingDouble(User: :getPrice))
);

可以对结果进行统计
Map < String,Long > collect = users.stream().collect(
Collectors.groupingBy(User: :getEdu,
    //获取count数量
Collectors.counting())
);
  • 需要三个参数,第三个参数添加了对结果Map的生成方式,默认是HashMap
Map <String,Double > collect = users.stream().collect(
Collectors.groupingBy(User: :getEdu,
    //决定map的生成方式,使用TreeMap
    TreeMap: :new,
    //对参数进行累计求和
    Collectors.summingDouble(User: :getPrice))
);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值