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中合并两个对象列表


- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 转换 ldap 日期 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 获取数字的最后一位 2022-01-01