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 <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 = Lists.newArrayList();
List<V> listAll = Lists.newArrayList();
List<K> resultKey = Lists.newArrayList();
Set<K> setLeft = convert(left, function);
Set<K> 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<K, V> multimaps = Multimaps.index(listAll, function);
for (int i = 0; i < resultKey.size(); i++) {
ImmutableList<V> immutableList = (ImmutableList<V>) 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 <K> List<K> getDifference(Set<K> setLeft, Set<K> setRight) {
Sets.SetView setView = Sets.difference(setLeft, setRight);
return setView.immutableCopy().asList();
}
private static <K> List<K> getSymmetricDifference(Set<K> setLeft, Set<K> setRight) {
Sets.SetView setView = Sets.symmetricDifference(setLeft, setRight);
return setView.immutableCopy().asList();
}
private static <K> List<K> getUnion(Set<K> setLeft, Set<K> setRight) {
Sets.SetView setView = Sets.union(setLeft, setRight);
return setView.immutableCopy().asList();
}
private static <K> List<K> getIntersection(Set<K> setLeft, Set<K> setRight) {
Sets.SetView setView = Sets.intersection(setLeft, setRight);
return setView.immutableCopy().asList();
}
private static <K, V> Set<K> convert(Iterable<V> value, Function<V, K> function) {
Set set = new HashSet();
Iterator<V> values = value.iterator();
while (values.hasNext()) {
V v = values.next();
set.add(function.apply(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 = Lists.newArrayList();
Map<K, V> map = Maps.newHashMap();
Iterator<V> 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<String> list1 = new ArrayList<>();
ArrayList<String> 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<String> getInter(List<String> works, List<String> otherDays) {
if (CollectionUtil.isEmpty(works) || CollectionUtil.isEmpty(otherDays)) {
return new ArrayList<>();
}
Function<String, String> function = new Function<String, String>() {
@Override
public String apply(String from) {
// 拼接list中比较的字段
return from;
}
};
List<String> differenceList = ListUtils.getDifferenceList(works, otherDays, CpType.InterLeft, function);
return differenceList;
}
}
使用实例(point 点位模型 里面包含一些地址属性类 就不罗列了)
自己实现去比较函数 改函数需要返回 需要比较哪些属性值
注意导包使用谷歌的function
import com.google.common.base.Function;
Function<Point, String> function = new Function<Point, String>() {
@Override
public String apply(Point from) {
// 拼接list中需要比较的字段
return from.getProvince() + from.getCity() + from.getArea() + from.getStreet();
}
};
List<Point> newPointList = ListUtils.getDifferenceList(listPointsLeft, listPointsRight, ListUtils.CpType.OnlyRight, function);
listPointsLeft listPointsRight l两个需要比较的list
ListUtils.CpType.OnlyRight 去重后保留那边的数据
function 比较函数
二、单个list去重
public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
Map<Object, Boolean> 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();