Java-Java集合流操作


修订记录 版本 是否发布
2020-01-25 v1.0
2021-03-19 v1.1

List分组

Map<String,List<ClassEntity>> = classEntities.stream().collect(Collectors.groupingBy(ClassEntity::getGrade));

List去重

1、单字段去重

petList = petList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(
                () -> new TreeSet<>(Comparator.comparing(PetsPetInfo::getUserId))), ArrayList::new));

2、多字段去重

java8去重(根据年级和专业,当年级和专业都相同的情况下看做是重复数据)

List<ClassEntity> distinctClass = classEntities.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getProfessionId() + ";" + o.getGrade()))), ArrayList::new));

通过hashSet去重(如将classNames去重):该种去重是bean完全相同的时候算重复数据

List<String> classNameList = new ArrayList(new HashSet(classNames));

List交集方法retainAll

public class Test {

    public static void main(String[] args) {

        List<String> list1 = new ArrayList<String>();
        List<String> list2 = new ArrayList<String>();
        List<String> list3 = new ArrayList<String>();
        for (int i = 0; i < 20; i++) {
            list1.add(i+"");
            if(i%2 == 0) {
                list2.add(i+"");
            }
            list3.add(i+"@");
        }
        // list1 与 list2 存在相同元素,list1集合只保留list2中存在的元素
        list1.retainAll(list2);
        if(list1.isEmpty()) {
            System.out.println("不包含");
        } else {
            System.out.println("包含");
        }
        System.out.println(list1);
        // list1 与 list3 不存在相同元素,list1集合变为空
        list1.retainAll(list3);
        if(list1.isEmpty()) {
            System.out.println("不包含");
        } else {
            System.out.println("包含");
        }
        System.out.println(list1);
    }
}

List 分组去重获取最新记录

public static void main(String[] args) throws InterruptedException {
    List<VehicleOrder> vehicleOrderBqList = Lists.newArrayList();
    VehicleOrder v1 = new VehicleOrder();
    v1.setDealerCode("BQ0062");
    v1.setCustomerPhone("13101030009");
    v1.setReceivedTotalMoney(new BigDecimal(0));
    v1.setUpdateTime(DateUtils.getNowLocalDateTime());
    Thread.sleep(2000);

    VehicleOrder v2 = new VehicleOrder();
    v2.setDealerCode("BQ0062");
    v2.setCustomerPhone("13101030009");
    v2.setReceivedTotalMoney(new BigDecimal(0));
    v2.setUpdateTime(DateUtils.getNowLocalDateTime());
    Thread.sleep(2000);

    VehicleOrder v3 = new VehicleOrder();
    v3.setDealerCode("BQ0062");
    v3.setCustomerPhone("13101030009");
    v2.setReceivedTotalMoney(new BigDecimal(100));
    v3.setUpdateTime(DateUtils.getNowLocalDateTime());
    Thread.sleep(2000);

    VehicleOrder v4 = new VehicleOrder();
    v4.setDealerCode("BQ0062");
    v4.setCustomerPhone("13101030009");
    v4.setReceivedTotalMoney(new BigDecimal(500));
    v4.setUpdateTime(DateUtils.getNowLocalDateTime());
    Thread.sleep(2000);

    vehicleOrderBqList.add(v1);
    vehicleOrderBqList.add(v2);
    vehicleOrderBqList.add(v3);
    vehicleOrderBqList.add(v4);

    //方式一:分组随机去重
/*    List<VehicleOrder> distinctList = vehicleOrderBqList.parallelStream()
        .collect(Collectors.collectingAndThen(Collectors.toCollection(
            () -> new TreeSet<>(Comparator.comparing(en -> en.getDealerCode() + ";" + en.getCustomerPhone()))),
            ArrayList::new));*/

    //方式二:分组去重,根据UpdateTime获取最新记录(去重规则)
    List<VehicleOrder> distinctList = Lists.newArrayList();
    distinctList = vehicleOrderBqList.parallelStream()
        .collect(Collectors.groupingBy(en -> en.getDealerCode() + ";" + en.getCustomerPhone())).values()
        .parallelStream().map(list -> list.parallelStream().max(Comparator.nullsLast(Comparator.comparing(VehicleOrder::getUpdateTime))).orElse(null)
        ).collect(Collectors.toList());

   //方式三:分组去重,根据UpdateTime获取最新记录(去重规则)
/*    vehicleOrderBqList.parallelStream()
        .collect(Collectors.groupingBy(en -> en.getDealerCode() + ";" + en.getCustomerPhone())).forEach((k, v) -> {
      VehicleOrder vvv = v.parallelStream()
          .max(Comparator.nullsLast(Comparator.comparing(VehicleOrder::getUpdateTime))).orElse(null);
      if (Optional.ofNullable(vvv).isPresent()) {
        distinctList.add(vvv);
      }
      ;
    });*/

    System.out.println(distinctList);

  }

List 分组去重获取最新记录

// 若value为空,设置默认值""
Map<String, String> map1 = list.stream().collect(
                Collectors.toMap(i -> i.getName(), i-> Optional.ofNullable(i.getSex()).orElse(""), (v1, v2) -> v1));

// 调用hashMap putAll方法, 注意key相同时,value会覆盖。
Map<String, String> map2 = list.stream().collect(
                HashMap::new, (m, i) -> m.put(i.getName(), i.getSex()), HashMap::putAll);

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/281945.html

(0)
上一篇 2022年8月24日
下一篇 2022年8月24日

相关推荐

发表回复

登录后才能评论