在Java 8中合并两个对象列表
- 论坛
- 在Java 8中合并两个对象列表
10 浏览
在Java 8中合并两个对象列表
我有一个Java类Parent
,有20个属性(attrib1, attrib2 .. attrib20)
以及对应的getter和setter方法。我还有两个Parent
对象的列表:list1
和list2
。
现在我想要合并这两个列表,并根据attrib1
和attrib2
避免重复的对象。
使用Java 8:
Listresult = Stream.concat(list1.stream(), list2.stream()) .distinct() .collect(Collectors.toList());
但是在哪个地方我需要指定这些属性?我应该重写hashCode
和equals
方法吗?