java 关于多个list去重处理的方法

list 比较去重有多种方法可以使用,我今天给大家介绍一种比较使用的,
一、util 方法

下面是工具类源代码

import cn.hutool.core.collection.CollectionUtil;
import com.google.common.base.Function;
import com.google.common.collect.*;

import java.util.*;

/**
 * @author : kangpt
 * @date : 2023-10-11 10:23
 */
public class ListUtils {

    public enum CpType {
        OnlyLeft,   // 左边有右边无
        OnlyRight,  // 右边有左边无
        Diff,// 两边差异合集
        Union,      // 两边并集 返回两边所有数据 不做去重处理
        UnionLeft,      // 两边并集 重复数据以左边为主
        UnionRight,      // 两边并集 重复数据以右边为主
        Inter,      // 两边交集 重复数据两个集合都返回
        InterLeft,    // 两边交集 重复数据以左边为主
        InterRight      // 两边交集 重复数据以右边为主


    }

    /**
     * 功能说明:两个集合比较
     *
     * @param left     集合1
     * @param right    集合2
     * @param cpType   CpType 比较类型
     * @param function 实现生成对象比较的方式
     * @param 
     * @param 
     * @return
     */
    public static  List getDifferenceList(List left, List right, CpType cpType, Function function) {

        List result = Lists.newArrayList();
        List listAll = Lists.newArrayList();

        List resultKey = Lists.newArrayList();
        Set setLeft = convert(left, function);
        Set setRight = convert(right, function);
        Boolean onlyOne = Boolean.FALSE;

        switch (cpType) {
            case OnlyLeft:
                listAll.addAll(left);
                resultKey = getDifference(setLeft, setRight);
                break;
            case OnlyRight:
                listAll.addAll(right);
                resultKey = getDifference(setRight, setLeft);
                break;
            case Diff:
                listAll.addAll(left);
                listAll.addAll(right);
                resultKey = getSymmetricDifference(setRight, setLeft);
                break;
            case Union:
                listAll.addAll(left);
                listAll.addAll(right);
                resultKey = getUnion(setLeft, setRight);
                break;
            case UnionLeft:
                listAll.addAll(left);
                listAll.addAll(right);
                onlyOne = Boolean.TRUE;
                resultKey = getUnion(setLeft, setRight);
                break;
            case UnionRight:
                listAll.addAll(right);
                listAll.addAll(left);
                onlyOne = Boolean.TRUE;
                resultKey = getUnion(setLeft, setRight);
                break;
            case Inter:
                listAll.addAll(left);
                listAll.addAll(right);
                resultKey = getIntersection(setLeft, setRight);
                break;
            case InterLeft:
                listAll.addAll(left);
                resultKey = getIntersection(setLeft, setRight);
                break;
            case InterRight:
                listAll.addAll(right);
                resultKey = getIntersection(setLeft, setRight);
                break;

        }

        Multimap multimaps = Multimaps.index(listAll, function);
        for (int i = 0; i < resultKey.size(); i++) {
            ImmutableList immutableList = (ImmutableList) multimaps.get(resultKey.get(i));
            for (int j = 0; j < immutableList.size(); j++) {
                if (onlyOne && j != 0) {
                    continue;
                }
                result.add(immutableList.get(j));
            }
        }

        return result;
    }


    private static  List getDifference(Set setLeft, Set setRight) {
        Sets.SetView setView = Sets.difference(setLeft, setRight);
        return setView.immutableCopy().asList();
    }

    private static  List getSymmetricDifference(Set setLeft, Set setRight) {
        Sets.SetView setView = Sets.symmetricDifference(setLeft, setRight);
        return setView.immutableCopy().asList();
    }

    private static  List getUnion(Set setLeft, Set setRight) {
        Sets.SetView setView = Sets.union(setLeft, setRight);
        return setView.immutableCopy().asList();
    }

    private static  List getIntersection(Set setLeft, Set setRight) {
        Sets.SetView setView = Sets.intersection(setLeft, setRight);
        return setView.immutableCopy().asList();
    }

    private static  Set convert(Iterable value, Function function) {
        Set set = new HashSet();
        Iterator values = value.iterator();
        while (values.hasNext()) {
            V v = values.next();
            set.add(function.apply(v));
        }
        return set;
    }


    /**
     * 功能说明:根据规则判断 Iterable 是否含有相同对象,如果有返回相同的对象。
     *
     * @param value
     * @param function
     * @param 
     * @param 
     * @return
     */
    public static  List checkListRepetition(Iterable value, Function function) {
        List list = Lists.newArrayList();
        Map map = Maps.newHashMap();
        Iterator values = value.iterator();
        while (values.hasNext()) {
            V v = values.next();
            K k = function.apply(v);
            if (null != map.get(k)) {
                list.add(map.get(k));
                list.add(v);
            }
            map.put(k, v);
        }
        return list;
    }

    public static void main(String[] args) {
        ArrayList list1 = new ArrayList<>();
        ArrayList list2 = new ArrayList<>();
        list1.add("1");
        list1.add("2");
        list1.add("3");

        list2.add("1");
        list2.add("2");
        list2.add("4");
        System.out.println(getInter(list1, list2));
    }

    private static List getInter(List works, List otherDays) {
        if (CollectionUtil.isEmpty(works) || CollectionUtil.isEmpty(otherDays)) {
            return new ArrayList<>();
        }
        Function function = new Function() {
            @Override
            public String apply(String from) {
                // 拼接list中比较的字段
                return from;
            }
        };
        List differenceList = ListUtils.getDifferenceList(works, otherDays, CpType.InterLeft, function);
        return differenceList;
    }
}

使用实例(point 点位模型  里面包含一些地址属性类 就不罗列了)

自己实现去比较函数  改函数需要返回 需要比较哪些属性值

注意导包使用谷歌的function

import com.google.common.base.Function;
Function function = new Function() {
    @Override
    public String apply(Point from) {
        // 拼接list中需要比较的字段
        return from.getProvince() + from.getCity() + from.getArea() + from.getStreet();
    }
};

List newPointList = ListUtils.getDifferenceList(listPointsLeft, listPointsRight, ListUtils.CpType.OnlyRight, function);

listPointsLeft  listPointsRight  l两个需要比较的list 
ListUtils.CpType.OnlyRight 去重后保留那边的数据
function 比较函数

二、单个list去重

public static  Predicate distinctByKey(Function keyExtractor) {
    Map seen = new ConcurrentHashMap<>();
    return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}

使用实例:

long count = listPoints.stream().filter(distinctByKey(item -> item.getProvince() + item.getCity() + item.getArea() + item.getStreet()))
        .collect(Collectors.toList()).size();

你可能感兴趣的:(java)