关于以下方法,直接可以在自己项目创建一个类,然后进行测试使用,去重方式有很多种,文本末尾也提供了每个方法的运行结果,供大家参考使用!
文章大量使用到了Java8当中的Lambda表达式,以及stream流相关基础知识。如果您用的不熟,没关系,可以直接复制下面的方案然后进行套着使用即可,如果想深入了解的,可以看一下这两篇文章:
Lambda讲解:https://blog.csdn.net/weixin_43888891/article/details/124795863
Stream讲解:https://blog.csdn.net/weixin_43888891/article/details/124657128
测试对象:
@Data
@AllArgsConstructor
@ToString
public class User {
@ApiModelProperty("姓名")
private String name;
@ApiModelProperty("性别")
private String gender;
@ApiModelProperty("年龄")
private Integer age;
@ApiModelProperty("出生日期")
private String birthday;
@ApiModelProperty("证件类型")
private String paperworkType;
@ApiModelProperty("证件号码")
private String identityNumber;
}
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class Test {
private static final List<User> list = Arrays.asList(
new User("张三", "男", 2, "2019-1-1", "身份证", "350781196403077840"),
new User("李四", "男", 2, "2019-1-1", "港澳证", "350781196403077840"),
new User("王五", "男", 9, "2019-1-1", "身份证", "350781196403077840"),
new User("赵六", "男", 8, "2019-1-1", "身份证", "350781196403077840"),
new User("赵六", "男", 8, "2019-1-1", "身份证", "350781196403077840"));
/**
* 对所有属性一样的数据进行去重
*/
public static void allColumnDistinct() {
List<User> collect = list.stream().distinct().collect(Collectors.toList());
collect.forEach(System.out::println);
}
/**
* 对单个属性去重,只返回去重的属性
*/
public static void columnDistinct() {
List<String> identityNumberList = list.stream().map(User::getIdentityNumber).distinct().collect(Collectors.toList());
identityNumberList.forEach(System.out::println);
}
/**
* 方式一:对单个属性一样的数据进行去重,下面是对身份证去重
*/
public static void oneColumnDistinct1() {
List<User> collect = list.stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(
Comparator.comparing(User::getIdentityNumber))), ArrayList::new));
collect.forEach(System.out::println);
}
/**
* 多个字段条件去重
*/
public static void twoColumnDistinct() {
List<User> collect = list.stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(
Comparator.comparing(p -> p.getIdentityNumber() + ";" + p.getPaperworkType()))), ArrayList::new));
collect.forEach(System.out::println);
}
/**
* 自定义属性判断
*
* @param keyExtractor
* @param <T>
* @return
*/
public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
Map<Object, Boolean> seen = new ConcurrentHashMap<>(1);
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
/**
* 方式二:对单个属性一样的数据进行去重,下面是对身份证去重
*/
public static void oneColumnDistinct2() {
List<User> collect = list.stream().filter(distinctByKey(b -> b.getIdentityNumber())).collect(Collectors.toList());
collect.forEach(System.out::println);
}
/**
* 方式三:对单个属性一样的数据进行去重,下面是对身份证去重
* 利用TreeSet进行去重
*/
public static void oneColumnDistinct3() {
TreeSet<User> collect = new TreeSet<>(Comparator.comparing(s -> s.getIdentityNumber()));
list.forEach(a -> collect.add(a));
collect.forEach(System.out::println);
}
public static void main(String[] args) {
System.out.println("-----------------allColumnDistinct---------------------");
allColumnDistinct();
System.out.println("-----------------columnDistinct---------------------");
columnDistinct();
System.out.println("-----------------oneColumnDistinct1---------------------");
oneColumnDistinct1();
System.out.println("-----------------twoColumnDistinct---------------------");
twoColumnDistinct();
System.out.println("-----------------oneColumnDistinct2---------------------");
oneColumnDistinct2();
System.out.println("-----------------oneColumnDistinct3---------------------");
oneColumnDistinct3();
}
}
输出结果:文章来源:https://www.toymoban.com/news/detail-560209.html
文章来源地址https://www.toymoban.com/news/detail-560209.html
到了这里,关于Java当中List集合根据对象某个属性进行去重的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!