stream对list数据进行多字段去重

方法一:

//根据sj和name去重
List<NursingHandover> testList = list.stream().collect(
                Collectors.collectingAndThen(Collectors.toCollection(
                        () -> new TreeSet<>(Comparator.comparing(
                                o -> o.getj() + ";" + o.getName() + ";")
                        )), ArrayList::new));

方法二:

// 通过Map生成键值对,Key去重
Map<Long, String> newMap = list.stream().collect(Collectors
        .toMap(NursingHandover::getSj, NursingHandover::getName));

方式三:

// 通过多个字段分组,并生成Map:key(字段组合),value(对象列表)
Map<Object, List<NursingHandover>> costLmmMap = list.parallelStream()
        .collect(Collectors.groupingBy(item -> (item.getSj() +
        "_" + item.getName()), Collectors.toList()));

你可能感兴趣的:(list,数据结构)