在Java 8中合并两个对象列表

10 浏览
0 Comments

在Java 8中合并两个对象列表

我有一个Java类Parent,有20个属性(attrib1, attrib2 .. attrib20)以及对应的getter和setter方法。我还有两个Parent对象的列表:list1list2

现在我想要合并这两个列表,并根据attrib1attrib2避免重复的对象。

使用Java 8:

List result = Stream.concat(list1.stream(), list2.stream())
                .distinct()
                .collect(Collectors.toList());   

但是在哪个地方我需要指定这些属性?我应该重写hashCodeequals方法吗?

0