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

时间:2025-03-10 14:19:15

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

下面是工具类源代码

import ;
import ;
import .*;

import .*;

/**
 * @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 <K>
     * @param <V>
     * @return
     */
    public static <K, V> List<V> getDifferenceList(List<V> left, List<V> right, CpType cpType, Function<V, K> function) {

        List<V> result = ();
        List<V> listAll = ();

        List<K> resultKey = ();
        Set<K> setLeft = convert(left, function);
        Set<K> setRight = convert(right, function);
        Boolean onlyOne = ;

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

        }

        Multimap<K, V> multimaps = (listAll, function);
        for (int i = 0; i < (); i++) {
            ImmutableList<V> immutableList = (ImmutableList<V>) ((i));
            for (int j = 0; j < (); j++) {
                if (onlyOne && j != 0) {
                    continue;
                }
                ((j));
            }
        }

        return result;
    }


    private static <K> List<K> getDifference(Set<K> setLeft, Set<K> setRight) {
         setView = (setLeft, setRight);
        return ().asList();
    }

    private static <K> List<K> getSymmetricDifference(Set<K> setLeft, Set<K> setRight) {
         setView = (setLeft, setRight);
        return ().asList();
    }

    private static <K> List<K> getUnion(Set<K> setLeft, Set<K> setRight) {
         setView = (setLeft, setRight);
        return ().asList();
    }

    private static <K> List<K> getIntersection(Set<K> setLeft, Set<K> setRight) {
         setView = (setLeft, setRight);
        return ().asList();
    }

    private static <K, V> Set<K> convert(Iterable<V> value, Function<V, K> function) {
        Set set = new HashSet();
        Iterator<V> values = ();
        while (()) {
            V v = ();
            ((v));
        }
        return set;
    }


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

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

        ("1");
        ("2");
        ("4");
        (getInter(list1, list2));
    }

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

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

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

注意导包使用谷歌的function

import ;
Function<Point, String> function = new Function<Point, String>() {
    @Override
    public String apply(Point from) {
        // 拼接list中需要比较的字段
        return () + () + () + ();
    }
};

List<Point> newPointList = (listPointsLeft, listPointsRight, , function);

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

二、单个list去重

public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
    Map<Object, Boolean> seen = new ConcurrentHashMap<>();
    return t -> ((t), ) == null;
}

使用实例:

long count = ().filter(distinctByKey(item -> () + () + () + ()))
        .collect(()).size();