说明
在业务开发中,经常会遇到这么一个场景:
- 从数据源获取一个数据集合List
- 对该数据集合,按照一定的条件排重
- 遍历处理排重过后的集合
在看前辈们的排重代码时,发现什么样的都有。有用Map排重的,有用Set排重的,不仅代码冗余,最关键的两个问题是:
- 排重后的顺序还不一定是List原有的顺序
- 是从前往后排重还是从后往前排重(数组AABB排重后,取下标1、3还是2、4的问题)
于是乎,我就写了一个完整的排重工具类。支持:
- 从前从后开始排重
- 排重后可进行排序,不排序则严格按照List原有顺序
- 自定义排重字符串来排重
代码
DuplicateStringBuilder
该接口用于第三点功能,自定义字符串排重。
/**
* 排重字符串生成接口
* 建议使用匿名类的方式实现该接口,提升代码可读性
*
* @param <T>
*/
public interface DuplicateStringBuilder<T> {
/**
* @param content List中的一个实体
* @return 需要返回一个排重用的字符串,比如取实体content中的某些字段的组合
*/
String build(T content);
}
RemoveDuplicateUtil
工具类主体
import java.util.*;
public class RemoveDuplicateUtil {
private RemoveDuplicateUtil() {
}
/**
* 自定义字段排重处理单个List内容的方法
*
* @param content List中的一个实体
* @param duplicateStringBuilder 排重字段拼接接口
* @param toList 排重结果List
* @param dupKeyList 排重字段List
* @param <T> List泛型
*/
private static <T> void removeByField(T content, DuplicateStringBuilder<T> duplicateStringBuilder, List<T> toList,
List<String> dupKeyList) {
String dupKey;
if (null == duplicateStringBuilder) {
dupKey = null;
} else {
dupKey = duplicateStringBuilder.build(content);
}
if (!dupKeyList.contains(dupKey)) {
dupKeyList.add(dupKey);
toList.add(content);
}
}
/**
* 自定义字段排重处理单个List内容的方法
*
* @param content List中的一个实体
* @param duplicateStringBuilder 排重字段拼接接口
* @param toMap 排重结果Map
* @param dupKeyList 排重字段List
* @param <T> List泛型
*/
private static <T> void removeByField(T content, DuplicateStringBuilder<T> duplicateStringBuilder,
HashMap<String, T> toMap,
List<String> dupKeyList) {
String dupKey;
if (null == duplicateStringBuilder) {
dupKey = null;
} else {
dupKey = duplicateStringBuilder.build(content);
}
if (!dupKeyList.contains(dupKey)) {
dupKeyList.add(dupKey);
toMap.put(dupKey, content);
}
}
/**
* 集合判空
*
* @param collection 集合
* @return 当集合为null 或 空集合 时,返回true
*/
private static <T> boolean isEmpty(Collection<T> collection) {
return null == collection || collection.isEmpty();
}
/**
* 从前往后排重,泛型T需重写equals方法。
* 使用Java自带的类型时推荐用此方法,如String,因为无需自己重写equals
*
* @param fromList 需要排重的List
* @param <T> 注意!!!当T为自定义的类时,需重写equals方法
* @return 排重过的List
*/
public static <T> List<T> ascRemove(List<T> fromList) {
List<T> toList = new ArrayList<>();
if (isEmpty(fromList)) {
return toList;
}
for (T content : fromList) {
if (!toList.contains(content)) {
toList.add(content);
}
}
return toList;
}
/**
* 从前往后排重并排序
*
* @param fromList 需要排重的List
* @param comparator 如果需要自定义排序的比较器,则需传入Comparator。传入null时,则使用泛型T实现的Comparable接口作比较器
* @param <T> 泛型T需重写equals方法,且需实现Comparable接口。
* @return 排重过的List
*/
public static <T extends Comparable> List<T> ascRemoveAndSort(List<T> fromList,
Comparator<T> comparator) {
List<T> toList = ascRemove(fromList);
Collections.sort(toList,comparator);
return toList;
}
/**
* List从前往后排重
* 使用自定义的类时推荐用此方法,对类的侵入性最小。
*
* @param fromList 需要排重的List
* @param duplicateStringBuilder 排重字段拼接接口,建议使用匿名类实现。
* @param <T> 无需重写任何方法,无需实现任何接口
* @return 排重过的List
*/
public static <T> List<T> ascRemoveByFields(List<T> fromList,
DuplicateStringBuilder<T> duplicateStringBuilder) {
List<T> toList = new ArrayList<>();
if (isEmpty(fromList)) {
return toList;
}
List<String> dupKeyList = new ArrayList<>();
for (T content : fromList) {
removeByField(content, duplicateStringBuilder, toList, dupKeyList);
}
return toList;
}
/**
* List从前往后排重并排序,排序规则按排重字符串字典升序
* 使用自定义的类时推荐用此方法,对类的侵入性最小。
*
* @param fromList 需要排重的List
* @param duplicateStringBuilder 排重字段拼接接口,建议使用匿名类实现。
* @param <T> 无需重写任何方法,无需实现任何接口
* @return 排重过的List
*/
public static <T> List<T> ascRemoveByFieldsAndSort(List<T> fromList,
DuplicateStringBuilder<T> duplicateStringBuilder) {
List<T> toList = new ArrayList<>();
if (isEmpty(fromList)) {
return toList;
}
HashMap<String,T> toMap = new HashMap<>();
List<String> dupKeyList = new ArrayList<>();
for (T content : fromList) {
removeByField(content, duplicateStringBuilder, toMap, dupKeyList);
}
Collections.sort(dupKeyList,null);
for (String key: dupKeyList) {
toList.add(toMap.get(key));
}
return toList;
}
/**
* 从后往前排重,泛型T需重写equals方法。
* 使用Java自带的类型时推荐用此方法,如String,因为无需自己重写equals
*
* @param fromList 需要排重的List
* @param <T> 注意!!!当T为自定义的类时,需重写equals方法
* @return 排重过的List
*/
public static <T> List<T> descRemove(List<T> fromList) {
List<T> toList = new ArrayList<>();
if (isEmpty(fromList)) {
return toList;
}
int size = fromList.size();
for (int i = size - 1; i >= 0; i--) {
T content = fromList.get(i);
if (!toList.contains(content)) {
toList.add(content);
}
}
return toList;
}
/**
* 从后往前排重 并排序
*
* @param fromList 需要排重的List
* @param comparator 如果需要自定义排序的比较器,则需传入Comparator。传入null时,则使用泛型T实现的Comparable接口作比较器
* @param <T> 泛型T需重写equals方法,且需实现Comparable接口。
* @return 排重过的List
*/
public static <T extends Comparable> List<T> descRemoveAndSort(List<T> fromList,
Comparator<T> comparator) {
List<T> toList = descRemove(fromList);
Collections.sort(toList,comparator);
return toList;
}
/**
* List从后往前排重
* 使用自定义的类时推荐用此方法,对类的侵入性最小。
*
* @param fromList 需要排重的List
* @param duplicateStringBuilder 排重字段拼接接口,建议使用匿名类实现。
* @param <T> 无需重写任何方法,无需实现任何接口
* @return 排重过的List
*/
public static <T> List<T> descRemoveByFields(List<T> fromList,
DuplicateStringBuilder<T> duplicateStringBuilder) {
List<T> toList = new ArrayList<>();
if (isEmpty(fromList)) {
return toList;
}
int size = fromList.size();
List<String> dupKeyList = new ArrayList<>();
for (int i = size - 1; i >= 0; i--) {
T content = fromList.get(i);
removeByField(content, duplicateStringBuilder, toList, dupKeyList);
}
return toList;
}
/**
* List从后往前排重并排序,排序规则按排重字符串字典升序
* 使用自定义的类时推荐用此方法,对类的侵入性最小。
*
* @param fromList 需要排重的List
* @param duplicateStringBuilder 排重字段拼接接口,建议使用匿名类实现。
* @param <T> 无需重写任何方法,无需实现任何接口
* @return 排重过的List
*/
public static <T> List<T> descRemoveByFieldsAndSort(List<T> fromList,
DuplicateStringBuilder<T> duplicateStringBuilder) {
List<T> toList = new ArrayList<>();
if (isEmpty(fromList)) {
return toList;
}
int size = fromList.size();
List<String> dupKeyList = new ArrayList<>();
HashMap<String,T> toMap = new HashMap<>();
for (int i = size - 1; i >= 0; i--) {
T content = fromList.get(i);
removeByField(content, duplicateStringBuilder, toMap, dupKeyList);
}
Collections.sort(dupKeyList,null);
for (String key: dupKeyList) {
toList.add(toMap.get(key));
}
return toList;
}
}
Test Demo
测试工具类
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class Test {
private static void testList1() {
List<People> peopleList = new ArrayList<>();
peopleList.add(new People("李四", 21));
peopleList.add(new People("王五", 22));
peopleList.add(new People("张三", 20));
peopleList.add(new People("张三", 23));
peopleList.add(new People("王五", 25));
peopleList.add(new People("李四", 24));
List<People> res1 = RemoveDuplicateUtil.ascRemove(peopleList);
List<People> res2 = RemoveDuplicateUtil.ascRemoveAndSort(peopleList, null);
List<People> res3 = RemoveDuplicateUtil.ascRemoveAndSort(peopleList, new Comparator<People>() {
@Override
public int compare(People o1, People o2) {
return o2.getAge() - o1.getAge();
}
});
List<People> res4 = RemoveDuplicateUtil.descRemove(peopleList);
List<People> res5 = RemoveDuplicateUtil.descRemoveAndSort(peopleList, null);
List<People> res6 = RemoveDuplicateUtil.descRemoveAndSort(peopleList, new Comparator<People>() {
@Override
public int compare(People o1, People o2) {
return o2.getAge() - o1.getAge();
}
});
}
private static void testList2() {
List<People> peopleList = new ArrayList<>();
peopleList.add(new People("张三B", 21));
peopleList.add(new People("张三C", 22));
peopleList.add(new People("张三A", 20));
peopleList.add(new People("张三A", 23));
peopleList.add(new People("张三C", 25));
peopleList.add(new People("张三B", 24));
List<People> res1 = RemoveDuplicateUtil.ascRemoveByFields(peopleList, new DuplicateStringBuilder<People>() {
@Override
public String build(People content) {
return content.getName();
}
});
List<People> res2 = RemoveDuplicateUtil.descRemoveByFields(peopleList, new DuplicateStringBuilder<People>() {
@Override
public String build(People content) {
return content.getName() + content.getAge();
}
});
List<People> res3 = RemoveDuplicateUtil.descRemoveByFields(peopleList, null);
List<People> res4 = RemoveDuplicateUtil.ascRemoveByFieldsAndSort(peopleList, new DuplicateStringBuilder<People>() {
@Override
public String build(People content) {
return content.getName();
}
});
List<People> res5 = RemoveDuplicateUtil.ascRemoveByFieldsAndSort(peopleList, null);
List<People> res6 = RemoveDuplicateUtil.descRemoveByFieldsAndSort(peopleList, new DuplicateStringBuilder<People>() {
@Override
public String build(People content) {
return content.getName();
}
});
List<People> res7 = RemoveDuplicateUtil.descRemoveByFieldsAndSort(peopleList, null);
}
public static void main(String[] args) {
testList1();
testList2();
}
}
class People implements Comparable<People> {
private String name;
private int age;
public People(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
/**
* 默认排序需要实现Comparable的compareTo方法。当comparator为null时,默认使用此方法进行排序
* 当然也可以自定义comparator方法来排序,如下面的descCompare方法
*/
@Override
public int compareTo(People other) {
return this.age - other.age;
}
/**
* contains 方法内部使用 equals方法来判断两个对象是否相同,
* 所以重写,这里假设当两个People的name属性相同时,即相同。
*/
@Override
public boolean equals(Object obj) {
if (obj instanceof People) {
People other = (People) obj;
return this.name.equals(other.name);
} else {
return false;
}
}
@Override
public String toString() {
return "People{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}