list排序根据某个字段去重

public static void main(String[] args) {
    List<BenefitTag> benefitList = new ArrayList<>();
    BenefitTag benefitTag1 = new BenefitTag();
    benefitTag1.setItemId("1");
    benefitTag1.setTypeId("50");
    benefitList.add(benefitTag1);

    BenefitTag benefitTag2 = new BenefitTag();
    benefitTag2.setItemId("2");
    benefitTag2.setTypeId("20");
    benefitList.add(benefitTag2);

    BenefitTag benefitTag3 = new BenefitTag();
    benefitTag3.setItemId("3");
    benefitTag3.setTypeId("40");
    benefitList.add(benefitTag3);

    BenefitTag benefitTag4 = new BenefitTag();
    benefitTag4.setItemId("4");
    benefitTag4.setTypeId("50");
    benefitList.add(benefitTag4);

    //转map了所以本身的排序乱了
    List<BenefitTag> benefitList2 = benefitList.stream()
                .collect(Collectors.toMap(BenefitTag::getTypeId, Function.identity(), (a, b) -> a))
                .values().stream().collect(Collectors.toList());
    System.out.println("benefitList2"+JSON.toJSONString(benefitList2));

    
    //后面覆盖前面
    List<BenefitTag> benefitList3 = benefitList.stream()
            .collect(Collectors.collectingAndThen
                    (Collectors.toCollection(() ->
                                    new TreeSet<>(Comparator.comparing(t -> t.getTypeId()))),
                            ArrayList::new
                    ));
    System.out.println("benefitList3"+JSON.toJSONString(benefitList3));


    //前面覆盖后面的
    List<BenefitTag> benefitList4 = benefitList.stream().filter(dis(x -> x.getTypeId())).collect(Collectors.toList());
    //1 20 40 50
    System.out.println("benefitList4"+JSON.toJSONString(benefitList4));

    //指定位置排序
    List<BenefitTag> feedBenefitSortOwnList = new ArrayList<>();
    BenefitTag benefitTagOwn1 = new BenefitTag();
    benefitTagOwn1.setItemId("20");
    benefitTagOwn1.setOrder(1);
    feedBenefitSortOwnList.add(benefitTagOwn1);

    sortBenefitTagOwnList(benefitList,feedBenefitSortOwnList);
    //1 20 40 50
    System.out.println("feedBenefitSortOwnList"+JSON.toJSONString(benefitList));

    //
    BigDecimal a = new BigDecimal(2000).divide(new BigDecimal(1000),0, RoundingMode.DOWN);
    System.out.println("a:"+a);
}

public static <T> Predicate<? super T> dis(Function<? super T, ?> key) {
    Map<Object, Boolean> seen = new ConcurrentHashMap<>();
    return t -> seen.putIfAbsent(key.apply(t), Boolean.TRUE) == null;
}

public static void sortBenefitTagOwnList(List<BenefitTag> targetList,List<BenefitTag> feedBenefitSortOwnList) {
    try {
        if (CollectionUtils.isEmpty(targetList) || CollectionUtils.isEmpty(feedBenefitSortOwnList)) {
            return;
        }
        Map<String, BenefitTag> targetMap = targetList.stream().collect(Collectors.toMap(BenefitTag::getItemId, Function.identity(), (key1, key2) -> key1));

        for(BenefitTag feedBenefitSortOwn : feedBenefitSortOwnList){
            //获取要自定义排序的利益点
            BenefitTag benefitTag = targetMap.get(feedBenefitSortOwn.getItemId());
            if(!Objects.equals(targetMap.get(feedBenefitSortOwn.getItemId()),null)){
                sortBenefitTagOwn(targetList,benefitTag,feedBenefitSortOwn.getOrder());
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

//根据某个字段排序
public static void sortBenefitTagList(List<BenefitTag> targetList, List<String> orderList) {
    try {
        if (CollectionUtils.isEmpty(targetList) || CollectionUtils.isEmpty(orderList)) {
            return;
        }

        targetList.sort(((o1, o2) -> {
            int io1 = orderList.indexOf(o1.getItemId());
            int io2 = orderList.indexOf(o2.getItemId());
            if (io1 != -1) {
                io1 = targetList.size() - io1;
            }
            if (io2 != -1) {
                io2 = targetList.size() - io2;
            }
            return io2 - io1;
        }));
    } catch (Exception e) {
        e.printStackTrace();
    }
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值