java list 删除对象的属性,根据Java 8中的属性从对象列表中删除重复项

I am trying to remove duplicates from a List of objects based on some property.

can we do it in a simple way using java 8

List employee

Can we remove duplicates from it based on id property of employee. I have seen posts removing duplicate strings form arraylist of string.

解决方案

You can get a stream from the List and put in in the TreeSet from which you provide a custom comparator that compares id uniquely.

Then if you really need a list you can put then back this collection into an ArrayList.

import static java.util.Comparator.comparingInt;

import static java.util.stream.Collectors.collectingAndThen;

import static java.util.stream.Collectors.toCollection;

...

List unique = employee.stream()

.collect(collectingAndThen(toCollection(() -> new TreeSet<>(comparingInt(Employee::getId))),

ArrayList::new));

Given the example:

List employee = Arrays.asList(new Employee(1, "John"), new Employee(1, "Bob"), new Employee(2, "Alice"));

It will output:

[Employee{id=1, name='John'}, Employee{id=2, name='Alice'}]

Another idea could be to use a wrapper that wraps an employee and have the equals and hashcode method based with its id:

class WrapperEmployee {

private Employee e;

public WrapperEmployee(Employee e) {

this.e = e;

}

public Employee unwrap() {

return this.e;

}

@Override

public boolean equals(Object o) {

if (this == o) return true;

if (o == null || getClass() != o.getClass()) return false;

WrapperEmployee that = (WrapperEmployee) o;

return Objects.equals(e.getId(), that.e.getId());

}

@Override

public int hashCode() {

return Objects.hash(e.getId());

}

}

Then you wrap each instance, call distinct(), unwrap them and collect the result in a list.

List unique = employee.stream()

.map(WrapperEmployee::new)

.distinct()

.map(WrapperEmployee::unwrap)

.collect(Collectors.toList());

In fact, I think you can make this wrapper generic by providing a function that will do the comparison:

class Wrapper {

private T t;

private Function equalityFunction;

public Wrapper(T t, Function equalityFunction) {

this.t = t;

this.equalityFunction = equalityFunction;

}

public T unwrap() {

return this.t;

}

@Override

public boolean equals(Object o) {

if (this == o) return true;

if (o == null || getClass() != o.getClass()) return false;

@SuppressWarnings("unchecked")

Wrapper that = (Wrapper) o;

return Objects.equals(equalityFunction.apply(this.t), that.equalityFunction.apply(that.t));

}

@Override

public int hashCode() {

return Objects.hash(equalityFunction.apply(this.t));

}

}

and the mapping will be:

.map(e -> new Wrapper<>(e, Employee::getId))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值