原创

Java-代码段-list列表中去掉重复的数据-判重-按照一定的规则取舍

public static void main(String[] args) {
    //score 相同(其实就是0,) 取后面的
  // scroe 不同 ,scroe 大的
  List<EduTrainExamVo> eduTrainExamVos = Lists.newArrayList();
    EduTrainExamVo trainExamVo = new EduTrainExamVo();
    trainExamVo.setId(2);
    trainExamVo.setScore(0f);
    trainExamVo.setExamName("期望保留2");
    trainExamVo.setExamStatus("done");
    eduTrainExamVos.add(trainExamVo);


    trainExamVo = new EduTrainExamVo();
    trainExamVo.setId(2);
    trainExamVo.setScore(0f);
    trainExamVo.setExamName("期望保留2");
    eduTrainExamVos.add(trainExamVo);

    trainExamVo = new EduTrainExamVo();
    trainExamVo.setId(3);
    trainExamVo.setScore(0f);
    trainExamVo.setExamName("期望废弃3");
    eduTrainExamVos.add(trainExamVo);

    trainExamVo = new EduTrainExamVo();
    trainExamVo.setId(3);
    trainExamVo.setScore(10f);
    trainExamVo.setExamName("期望保留3");
    eduTrainExamVos.add(trainExamVo);


    trainExamVo = new EduTrainExamVo();
    trainExamVo.setId(4);
    trainExamVo.setScore(0f);
    trainExamVo.setExamName("期望保留4");
    eduTrainExamVos.add(trainExamVo);

    trainExamVo = new EduTrainExamVo();
    trainExamVo.setId(5);
    trainExamVo.setScore(55f);
    trainExamVo.setExamName("期望保留5");
    eduTrainExamVos.add(trainExamVo);

    trainExamVo = new EduTrainExamVo();
    trainExamVo.setId(2);
    trainExamVo.setScore(0f);
    trainExamVo.setExamName("期望保留2-2");
    eduTrainExamVos.add(trainExamVo);

    trainExamVo = new EduTrainExamVo();
    trainExamVo.setId(3);
    trainExamVo.setScore(0f);
    trainExamVo.setExamName("期望保留3-2");
    trainExamVo.setExamStatus("done");
    eduTrainExamVos.add(trainExamVo);

    List<EduTrainExamVo> eduTrainExamVosNoRepeat = removeRepeatVo(eduTrainExamVos);

    System.out.println(JSON.toJSONString(eduTrainExamVosNoRepeat));
}

private static List<EduTrainExamVo> removeRepeatVo(List<EduTrainExamVo> eduTrainExamVos) {
    if(CollectionUtils.isEmpty(eduTrainExamVos)){
        return Collections.emptyList();
    }
    ImmutableListMultimap<Integer, EduTrainExamVo> mapping = Multimaps.index(eduTrainExamVos, EduTrainExamVo::getId);
    List<EduTrainExamVo> eduTrainExamVosNoRepeat = Lists.newArrayList();
    ImmutableSet<Integer> keySet = mapping.keySet();
    for (Integer id : keySet) {
        ImmutableList<EduTrainExamVo> list = mapping.get(id);
        if (list.size() > 1) {
            EduTrainExamVo target = null;
            for (EduTrainExamVo eduTrainExamVo : list) {
                if (target == null) {
                    target = eduTrainExamVo;
                    continue;
                }
         if (eduTrainExamVo.getScore().equals(target.getScore())) {
                    //score 相同(其实就是0,) 取后面的
           target = eduTrainExamVo;
                } else if (eduTrainExamVo.getScore() > target.getScore()) {
                    // score 不同 , 取score 大的
           target = eduTrainExamVo;
                }
            }
            if (target != null) {
                eduTrainExamVosNoRepeat.add(target);
            }
        } else if (list.size() == 1) {
            eduTrainExamVosNoRepeat.add(list.get(0));
        }
    }
    return eduTrainExamVosNoRepeat;
}
正文到此结束
本文目录