Java 8 List 如何根据对象的属性去重

一、去除List中重复的String

public List<String> removeStringListDupli(List<String> stringList) {
    Set<String> set = new LinkedHashSet<>();
    set.addAll(stringList);

    stringList.clear();

    stringList.addAll(set);
    return stringList;
}

//或使用Java8的写法:

List<String> unique = list.stream().distinct().collect(Collectors.toList());

二、List中对象去重

重写Person对象的equals()方法和hashCode()方法:

 @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Person person = (Person) o;

        if (!id.equals(person.id)) return false;
        return name.equals(person.name);

    }

    @Override
    public int hashCode() {
        int result = id.hashCode();
        result = 31 * result + name.hashCode();
        return result;
    }

下面对象去重的代码:

Person p1 = new Person(1l, "jack");
        Person p2 = new Person(3l, "jack");
        Person p3 = new Person(2l, "toccds");
        Person p4 = new Person(4l, "hacsdcdscsdnson");
        Person p5 = new Person(5l, "多少吃多少");

        List<Person> persons = Arrays.asList(p1, p2, p3, p4, p5, p5, p1, p2, p2);

        List<Person> personList = new ArrayList<>();
        // 去重
        persons.stream().forEach(
                p -> {
                    if (!personList.contains(p)) {
                        personList.add(p);
                    }
                }
        );
        System.out.println(personList);

List 的contains()方法底层实现使用对象的equals方法去比较的,其实重写equals()就好,但重写了equals最好将hashCode也重写了。

 还有链式写法:(推荐)


//1. 对于list对象去重
List<Person> personList = persons
.stream()
.collect(Collectors.toSet())
.stream()
.collect(Collectors.toList());

//当然还可以这样
List<Person> personList = persons
.stream()
.collect(Collectors
.collectingAndThen(Collectors
.toCollection(HashSet::new), ArrayList::new));

三、根据对象的属性去重

通过Comparator比较器,比较对象属性,相同就返回0,达到过滤的目的。

  public static List<Person> removeDupliById(List<Person> persons) {
        Set<Person> personSet = new TreeSet<>((o1, o2) -> o1.getId().compareTo(o2.getId()));
        personSet.addAll(persons);

        return new ArrayList<>(personSet);
    }

Java8写法:

// 根据id去重
     List<Person> unique = persons.stream().collect(
                collectingAndThen(
                        toCollection(() -> new TreeSet<>(comparingLong(Person::getId))), ArrayList::new)
        );

另一种写法:

public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
        Map<Object, Boolean> map = new ConcurrentHashMap<>();
        return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }

// remove duplicate
        persons.stream().filter(distinctByKey(p -> p.getId())).forEach(p -> System.out.println(p));

链式写法:(推荐)

List<Person> unique = persons
.stream()
.collect(Collectors
.toMap(Person::getId,
Function.identity(),
(o1, o2) -> o1))
.entrySet()
.stream()
.map(Map.Entry::getValue)
.collect(Collectors.toList());

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值