在这里我来分享几种列表去重的方法,算是一次整理吧,若有纰漏,请不吝赐教。java
distinct()
方法distinct()
是Java 8 中 Stream 提供的方法,返回的是由该流中不一样元素组成的流。distinct()
使用 hashCode()
和 eqauls()
方法来获取不一样的元素。所以,须要去重的类必须实现 hashCode()
和 equals()
方法。换句话讲,咱们能够经过重写定制的 hashCode()
和 equals()
方法来达到某些特殊需求的去重。git
distinct()
方法声明以下:github
Stream<T> distinct();
复制代码
String
列表的去重由于 String
类已经覆写了 equals()
和 hashCode()
方法,因此能够去重成功。bash
@Test
public void listDistinctByStreamDistinct() {
// 1. 对于 String 列表去重
List<String> stringList = new ArrayList<String>() {{
add("A");
add("A");
add("B");
add("B");
add("C");
}};
out.print("去重前:");
for (String s : stringList) {
out.print(s);
}
out.println();
stringList = stringList.stream().distinct().collect(Collectors.toList());
out.print("去重后:");
for (String s : stringList) {
out.print(s);
}
out.println();
}
复制代码
结果以下:app
去重前:AABBC
去重后:ABC
复制代码
注:代码中咱们使用了 Lombok
插件的 @Data
注解,可自动覆写 equals()
以及 hashCode()
方法。性能
/** * 定义一个实体类 */
@Data
public class Student {
private String stuNo;
private String name;
}
复制代码
@Test
public void listDistinctByStreamDistinct() throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
// 1. 对于 Student 列表去重
List<Student> studentList = getStudentList();
out.print("去重前:");
out.println(objectMapper.writeValueAsString(studentList));
studentList = studentList.stream().distinct().collect(Collectors.toList());
out.print("去重后:");
out.println(objectMapper.writeValueAsString(studentList));
}
复制代码
结果以下:spa
去重前:[{"stuNo":"001","name":"Tom"},{"stuNo":"002","name":"Mike"},{"stuNo":"001","name":"Tom"}]
去重后:[{"stuNo":"001","name":"Tom"},{"stuNo":"002","name":"Mike"}]
复制代码
List<Object>
中 Object
某个属性去重@Test
public void distinctByProperty1() throws JsonProcessingException {
// 这里第一种方法咱们经过新建立一个只有不一样元素列表来实现根据对象某个属性去重
ObjectMapper objectMapper = new ObjectMapper();
List<Student> studentList = getStudentList();
out.print("去重前 :");
out.println(objectMapper.writeValueAsString(studentList));
studentList = studentList.stream().distinct().collect(Collectors.toList());
out.print("distinct去重后:");
out.println(objectMapper.writeValueAsString(studentList));
// 这里咱们引入了两个静态方法,以及经过 TreeSet<> 来达到获取不一样元素的效果
// 1. import static java.util.stream.Collectors.collectingAndThen;
// 2. import static java.util.stream.Collectors.toCollection;
studentList = studentList.stream().collect(
collectingAndThen(
toCollection(() -> new TreeSet<>(Comparator.comparing(Student::getName))), ArrayList::new)
);
out.print("根据名字去重后 :");
out.println(objectMapper.writeValueAsString(studentList));
}
复制代码
结果以下:插件
去重前 :[{"stuNo":"001","name":"Tom"},{"stuNo":"001","name":"Tom"},{"stuNo":"003","name":"Tom"}]
distinct去重后:[{"stuNo":"001","name":"Tom"},{"stuNo":"003","name":"Tom"}]
根据名字去重后 :[{"stuNo":"001","name":"Tom"}]
复制代码
filter()
方法咱们首先建立一个方法做为 Stream.filter()
的参数,其返回类型为 Predicate
,原理就是判断一个元素可否加入到 Set
中去,代码以下:code
private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
Set<Object> seen = ConcurrentHashMap.newKeySet();
return t -> seen.add(keyExtractor.apply(t));
}
复制代码
使用以下:对象
@Test
public void distinctByProperty2() throws JsonProcessingException {
// 这里第二种方法咱们经过过滤来实现根据对象某个属性去重
ObjectMapper objectMapper = new ObjectMapper();
List<Student> studentList = getStudentList();
out.print("去重前 :");
out.println(objectMapper.writeValueAsString(studentList));
studentList = studentList.stream().distinct().collect(Collectors.toList());
out.print("distinct去重后:");
out.println(objectMapper.writeValueAsString(studentList));
// 这里咱们将 distinctByKey() 方法做为 filter() 的参数,过滤掉那些不能加入到 set 的元素
studentList = studentList.stream().filter(distinctByKey(Student::getName)).collect(Collectors.toList());
out.print("根据名字去重后 :");
out.println(objectMapper.writeValueAsString(studentList));
}
复制代码
结果以下:
去重前 :[{"stuNo":"001","name":"Tom"},{"stuNo":"001","name":"Tom"},{"stuNo":"003","name":"Tom"}]
distinct去重后:[{"stuNo":"001","name":"Tom"},{"stuNo":"003","name":"Tom"}]
根据名字去重后 :[{"stuNo":"001","name":"Tom"}]
复制代码
以上即是我要分享的几种关于列表去重的方法,固然这里没有进行更为详尽的性能分析,但愿之后会深刻底层再从新分析一下。若有纰漏,还望不吝赐教。
代码地址:github