java集合lamda常用一些方式

个人最近常使用集合lamda,大致整理下

  1. Map的values快速转List
    Map<String,Obj> diagnosisInvoiceMap = new ConcurrentHashMap<>();
    List list = diagnosisInvoiceMap.values().stream().collect(Collectors.toList();

  2. 集合的对象某个字段数据转速转Set
    List dataList = new ArrayList();
    Set problemSet = dataList.stream().map(e->e.getFile_id()).collect(Collectors.toSet());

  3. 集合数据过滤
    List detaillist = costDetailDtoList.stream().filter(e -> e.getInvoiceNo().equals(hospitalInvoice.getInvoiceNo()) ).collect(Collectors.toList());

  4. 判断集合是否过滤到数据(orelse)
    (1)第一种方式
    List dataList = new ArrayList();
    Optional first = dataList ().stream().filter(e -> e.getDrug_name().equals(costDetailDto.getProjectName())).findFirst();
    if(null == first || !first.isPresent()){
    //没有数据
    continue;
    }
    drugListDto = first.get();
    (2)第二种方式
    List dataList = new ArrayList();
    Stream.of(dataList.values()).filter(e -> e.getType().equals(type)).findFirst().orElse(null);
    (3). 第三种方式
    booolean flag = dataList ().stream().anyMach(data->data.equals(“3”)); //另:allMach 意思全部是否等于3,noneMatch正好相反
    为ture为全匹配上

  5. 集合数据,转Map:按照集合的某一个属性作为key,把对象作为对象
    (1)方法1
    List dataList = new ArrayList();
    Map<String, Obj> invoiceMap = dataList.collect(Collectors.toMap(paramCalcrate -> paramCalcrate.getInvoiceNo(), Function.identity()));
    (2)方法2
    Map<String, String> attrMap = dataList.stream().collect(Collectors.toMap(Obj::getName, Obj::getCity));
    (3) 方法3
    Map<String, Person> mapByName = persons.stream().collect(Collectors.toMap(Person::getName, p -> p));

  6. 集合对象的对象某个字段快速这个字段的集合
    List dataList = new ArrayList();
    List diagCodeList = dataList.map(e->e.getDiagnosisName()).filter(StringUtils::isNotBlank).collect(Collectors.toList());

  7. 集合对象的金额属性统计汇总
    List dataList = new ArrayList();
    BigDecimal payAmountSum = dataList.map(AdjustAndPerson::getArealAmount)
    .reduce(BigDecimal.ZERO, BigDecimal::add);
    另:dataList.stream().mapToInt(o -> Objects.isNull(o.getAmount()) ? 0 : o.getAmount()).sum())

  8. 集合对象的对象属性字段的以“,”分割
    List dataList = new ArrayList();
    String content = dataList.stream().map(x -> x.getProjectName() + “|” + x.getAmount()).collect(Collectors.joining(“,”));

  9. .集合按照集合对象某个属性分组group
    (1) 方法1
    List dataList = new ArrayList();
    Map<String, List> groupBySex = dataList.collect(Collectors.groupingBy(Obj::getItemName));
    (2) 方法2,自定义
    Map<Object, List> cityGroupSelf = dataList .stream()
    .collect(Collectors.groupingBy(obj -> {
    if (“北京”.equals(person.getCity()) || “广州”.equals(person.getCity())) {
    return “北广”;
    } else {
    return person.getCity();
    }
    }));
    });

  10. 集合的某个字段排序
    (1)方法1
    List dataList = new ArrayList();
    List result = dataList.values().stream().sorted(
    // 优先1
    Comparator.comparing(GetDuty::getDutyType, Comparator.reverseOrder())
    // 优先2
    .thenComparing(GetDuty::getDutyClass)
    // 优先3
    .thenComparing(GetDuty::getTreatType, Comparator.reverseOrder());
    (2)方法2
    List personsSorted = dataList .stream().sorted((a, b) -> {
    return (int) (a.getBirthday().getTime() - b.getBirthday().getTime());
    }).collect(Collectors.toList());

  11. 集合遍历
    List dataList = new ArrayList();
    dataList.forEach(person -> {
    person.setName(person.getName() + “@”);
    });

  12. 属性去重
    (1)方法1
    List dataList = new ArrayList();
    List names = dataList.stream().map(p->p.getName()).collect(Collectors.toList());
    System.out.println(“list的属性列表”+names);
    System.out.println(“去重后的列表:”+names.stream().distinct().collect(Collectors.toList()));
    (2)方法2
    TreeSet set = new TreeSet<>(dataList );
    System.out.println(“去重集合:” + set);

  13. 集合数据取最大值
    List dataList = new ArrayList(“1”,“2”,“3”);
    int maxValue = dataList.stream().max(Comparator.comparing(Integer,parseInt)).get();

  14. 排序
    List result = standardMedicalCatalogDtoList.stream().sorted(
    Comparator.comparing(BStandardMedicalCatalogDto::getCoincidentWordScore, Comparator.reverseOrder())
    .thenComparing(BStandardMedicalCatalogDto::getDrugClassSort)
    .thenComparing(BStandardMedicalCatalogDto::getMedicalCategory)
    ).collect(Collectors.toList());
    转参考资料:https://blog.csdn.net/qq_31635851/article/details/120322776

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值