java8: stream().map(),filter(),Collectors.toMap

  1. list赋值处理:.stream().map().distinct().collect(Collectors.toList())
List<TestDTO>  testList= testMapper.queryTestList(); testList = testList.stream().map(r -> { r.setNumber(r.getMumber+1); r.setName(r.getRemark()); return r; }).collect(Collectors.toList());
  1. filter()  过滤
testList = testList.stream()
        .filter(item -> ObjectUtils.nullSafeEquals(item.getTitle().trim(),
                orderItemInsertRequest.getProductName().trim()))
        .collect(Collectors.toList());

    Collectors.toMap使用详解

1.使用规则:

  1. toMap(Function, Function) 返回一个 Collector,它将元素累积到一个 Map中,其键和值是将提供的映射函数应用于输入元素的结果。

    如果映射的键包含重复项,则在执行收集操作时会抛出IllegalStateException。如果映射的键可能有重复项,请改用  toMap(Function, Function, BinaryOperator)。

    2.我们测试一下,首先新建一个Sdudent 类,三个属性分别是id,name,group

    然后构造一个List

            List<Student> list = new ArrayList<>();
    
            for (int i = 1; i < 4; i++) {
    
            list.add(new Student(i+"","学生"+i));
    
            }

    1. 将list转成以id为key的map,value是id对应的Sudent对象: 

    Map<String, Student> map = list.stream().collect(Collectors.toMap(Student::getId, Function.identity()));

    2.假如id存在重复值,则会报错Duplicate key xxx, 解决方案是:

    只取后一个key及value:

    Map<String, Student> map = list.stream().collect(Collectors.toMap(Student::getId,Function.identity(),(oldValue,newValue) -> newValue))

    只取前一个key及value:

    Map<String, Student> map = list.stream().collect(Collectors.toMap(Student::getId,Function.identity(),(oldValue,newValue) -> oldValue))

    3.想获得一个id和name对应的Map<String, String> :

    Map<String, String> map = list.stream().collect(Collectors.toMap(Student::getId,Student::getName)); 

    注意:name可以为空字符串但不能为null,否则会报空指针,解决方案:

    Map<String, String> map = list.stream().collect(Collectors.toMap(Student::getId, e->e.getName()==null?"":e.getName()));

    假如存在id重复,两个vaue可以这样映射到同一个id:

    Map<String, String> map = list.stream().collect(Collectors.toMap(Student::getId,Student::getName,(e1,e2)->e1+","+e2));

    4.把Student集合按照group分组到map中

    Map<String, List<Student>> map = list.stream().collect(Collectors.groupingBy(Student::getGroup));

    5.过滤去重,两个List<Student> 

    List<Student> list1 = new ArrayList<>();
    
    List<Student> list2= new ArrayList<>();
    
    HashMap<String, String> hashMap = new HashMap<>();
    
    for (int i = 1; i < 4; i++) {
    
        list1.add(new Student(i+"","学生"+i));
    
    }
    
    for (int i = 2; i < 5; i++) {
    
        list2.add(new Student(i+"","学生"+i));
    
    }
    
    Map<String, Student> map2 = list2.stream().collect(Collectors.toMap(Student::getId,Function.identity()));

    //把List1和List2中id重复的Student对象的name取出来:

    List<String> strings = list1.stream().map(Student::getId).filter(map2::containsKey).map(map2::get).map(Student::getName).collect(Collectors.toList());
    
    System.out.println(strings);// 输出 [学生2, 学生3]

    备注:Collectors.toMap使用详解
    Collectors.toMap使用详解 - 简书


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值