美文网首页
Java8对复杂对象distinct

Java8对复杂对象distinct

作者: 孙东民 | 来源:发表于2019-04-25 11:17 被阅读0次
  1. Java8对复杂对象distinct
  2. java8 - limit & filter &
  3. List 去重 | List对象去重 by ja
  4. 一次List对象去重失败,引发对Java8中distinct()
  5. java8 list 去重
  6. ListKit 一个基于Stream API的工具类
  7. Java8 Stream distinct 保留前面的还是后面的
  8. [DFS]78. Subsets
  9. Java基础篇之java8新特性:日期时间
  10. 【JAVA基础】Lambda-集合类处理
  11. 背景

    Java8使用Stream编写出来的代码相当简洁, 有时候需要使用distinct()方法去重, 针对比较复杂的对象的时候, distinct()就没法工作了.

    解决方案

    定义一个辅助方法:

    private static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
        Set<Object> seen = ConcurrentHashMap.newKeySet();
        return t -> seen.add(keyExtractor.apply(t));
    }
    

    然后通过如下调用:

    list.stream().filter(distinctByKey(Book::getName)).forEach(System.out::println);
    

    完整代码如下

    package com.github.stream;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Set;
    import java.util.concurrent.ConcurrentHashMap;
    import java.util.function.Function;
    import java.util.function.Predicate;
    
    public class DistinctByProperty {
        public static void main(String[] args) {
            List<Book> list = new ArrayList<>();
            list.add(new Book("Core Java", 200));
            list.add(new Book("Core Java", 300));
            list.add(new Book("Learning Freemarker", 150));
            list.add(new Book("Spring MVC", 200));
            list.add(new Book("Hibernate", 300));
    
            list.stream().filter(distinctByKey(Book::getName)).forEach(System.out::println);
        }
    
        private static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
            Set<Object> seen = ConcurrentHashMap.newKeySet();
            return t -> seen.add(keyExtractor.apply(t));
        }
    
        static class Book {
            private String name;
            private int price;
    
            public Book(String name, int price) {
                this.name = name;
                this.price = price;
            }
    
            public String getName() {
                return name;
            }
    
            public int getPrice() {
                return price;
            }
    
            @Override
            public String toString() {
                return "Book{" +
                        "name='" + name + '\'' +
                        ", price=" + price +
                        '}';
            }
        }
    }
    

    执行结果如下:

    Book{name='Core Java', price=200}
    Book{name='Learning Freemarker', price=150}
    Book{name='Spring MVC', price=200}
    Book{name='Hibernate', price=300}
    

    相关文章