Stream<T> distinct();
@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(); }
去重前:AABBC 去重后:ABC
/** * 定义一个实体类 */ @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)); }
去重前:[{"stuNo":"001","name":"Tom"},{"stuNo":"002","name":"Mike"},{"stuNo":"001","name":"Tom"}] 去重后:[{"stuNo":"001","name":"Tom"},{"stuNo":"002","name":"Mike"}]
@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"}]
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"}]
END 十期推荐 【231期】面试官:Java中 serialVersionUID 的作用是什么? 【232期】面试官:Spring的 IOC 容器比New对象究竟好在哪? 【233期】面试官:什么是耦合?解耦合的方法有哪几种? 【234期】30个 Java 集合面试必备的问题和答案 【235期】面试官:Redis的数据是存在内存里吗?谈谈Redis各种数据类型的使用场景? 【236期】面试官:线程池中多余的线程是如何回收的? 【237期】面试官:如何发现 Redis 热点 Key ,解决方案有哪些? 【238期】面试官:Redis新版本开始引入多线程,谈谈你的看法? 【239期】面试官:如何使用Redis实现电商系统的库存扣减? 【240期】面试官:你了解JVM的内存溢出吗? ? ~