Merging two List of objects in java 8(在java 8中合并两个对象列表)
问题描述
我有一个 Java 类 Parent
有 20 个属性 (attrib1, attrib2 .. attrib20)
及其对应的 getter 和 setter.我还有两个 Parent
对象列表:list1
和 list2
.
I have a Java class Parent
with 20 attributes (attrib1, attrib2 .. attrib20)
and its corresponding getters and setters. Also I have two lists of Parent
objects: list1
and list2
.
现在我想合并两个列表并避免基于 attrib1
和 attrib2
的重复对象.
Now I want to merge both lists and avoid duplicate objects based on attrib1
and attrib2
.
使用 Java 8:
List<Parent> result = Stream.concat(list1.stream(), list2.stream())
.distinct()
.collect(Collectors.toList());
但是我必须在哪个地方指定属性?我应该重写 hashCode
和 equals
方法吗?
But in which place I have to specify the attributes? Should I override hashCode
and equals
method?
推荐答案
如果要实现equals
和hashCode
,实现的地方在里面 类 Parent
.在该类中添加方法,如
If you want to implement equals
and hashCode
, the place to do it is inside the class Parent
. Within that class add the methods like
@Override
public int hashCode() {
return Objects.hash(getAttrib1(), getAttrib2(), getAttrib3(),
// …
getAttrib19(), getAttrib20());
}
@Override
public boolean equals(Object obj) {
if(this==obj) return true;
if(!(obj instanceof Parent)) return false;
Parent p=(Parent) obj;
return Objects.equals(getAttrib1(), p.getAttrib1())
&& Objects.equals(getAttrib2(), p.getAttrib2())
&& Objects.equals(getAttrib3(), p.getAttrib3())
// …
&& Objects.equals(getAttrib19(), p.getAttrib19())
&& Objects.equals(getAttrib20(), p.getAttrib20());
}
如果您这样做了,在 Stream
上调用的 distinct()
将自动执行正确的操作.
If you did this, distinct()
invoked on a Stream<Parent>
will automatically do the right thing.
如果您不想(或不能)更改类 Parent
,则没有相等的委托机制,但您可以使用 ordering有一个委托机制:
If you don’t want (or can’t) change the class Parent
, there is no delegation mechanism for equality, but you may resort to ordering as that has a delegation mechanism:
Comparator<Parent> c=Comparator.comparing(Parent::getAttrib1)
.thenComparing(Parent::getAttrib2)
.thenComparing(Parent::getAttrib3)
// …
.thenComparing(Parent::getAttrib19)
.thenComparing(Parent::getAttrib20);
这定义了基于属性的顺序.它要求属性本身的类型具有可比性.如果你有这样的定义,你可以使用它来实现一个 distinct()
的等价物,基于那个 Comparator
:
This defines an order based on the properties. It requires that the types of the attributes itself are comparable. If you have such a definition, you can use it to implement the equivalent of a distinct()
, based on that Comparator
:
List<Parent> result = Stream.concat(list1.stream(), list2.stream())
.filter(new TreeSet<>(c)::add)
.collect(Collectors.toList());
还有一个线程安全的变体,以防您想将它与并行流一起使用:
There is also a thread-safe variant, in case you want to use it with parallel streams:
List<Parent> result = Stream.concat(list1.stream(), list2.stream())
.filter(new ConcurrentSkipListSet<>(c)::add)
.collect(Collectors.toList());
这篇关于在java 8中合并两个对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在java 8中合并两个对象列表
基础教程推荐
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01