Java -合并两个列表,根据属性的值删除重复项



我有一个这样的类:

public class MyObject {
private Long id;
private String firstProp;
private String secondProp;
private Boolean isLatest;
}

在我的应用程序中,我得到两个列表,一个列表肯定将isLatest设置为(Boolean.TRUEBoolean.FALSE),另一个列表肯定将isLatest设置为null

我想要一个结果ListSet,其中包含两个列表中的项目,但在isLatest == null中删除了重复项。也就是说,在删除重复项时,我希望保留isLatest != null。副本的定义与id相同。

MyObject中的equals()hashCode()方法基于id,firstPropsecondProp,无法更改,因为它们是预定义的,更改它们会干扰应用程序的其他部分。

List<MyObject> listWithIsLatestSet = list with items below
myObj1 --> id = 1, isLatest = true;
myObj2 --> id = 2, isLatest = false;
myObj3 --> id = 3, isLatest = true;
myObj4 --> id = 4, isLatest = true;
List<MyObject> listWithIsLatestNull = list with items below
myObj5 --> id = 1, isLatest = null;
myObj6 --> id = 2, isLatest = null;
myObj7 --> id = 5, isLatest = null;
结果列表如下所示:
myObj1 --> id = 1, isLatest = true;
myObj2 --> id = 2, isLatest = false;
myObj3 --> id = 3, isLatest = true;
myObj4 --> id = 4, isLatest = true;
myObj7 --> id = 5, isLatest = null;

可以告诉我这个吗?

这个为我工作

import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class MAIN {
public static void main(String[] args) {
Set<MyObject> setWithIsLatestSet = getSetWithIsLatestSet();
Set<MyObject> setWithIsLatestNull = getSetWithIsLatestNull();
setWithIsLatestSet.addAll(setWithIsLatestNull);
setWithIsLatestSet.forEach(System.out::println);
}
private static Set<MyObject> getSetWithIsLatestSet() {
MyObject obj1 = MyObject.builder().id(new Long(1)).isLatest(Boolean.TRUE).build();
MyObject obj2 = MyObject.builder().id(new Long(2)).isLatest(Boolean.FALSE).build();
MyObject obj3 = MyObject.builder().id(new Long(3)).isLatest(Boolean.TRUE).build();
MyObject obj4 = MyObject.builder().id(new Long(4)).isLatest(Boolean.TRUE).build();
MyObject obj5 = MyObject.builder().id(new Long(5)).isLatest(Boolean.TRUE).build();
return Stream.of(obj1, obj2, obj3, obj4, obj5).collect(Collectors.toCollection(HashSet::new));
}
private static Set<MyObject> getSetWithIsLatestNull() {
MyObject obj1 = MyObject.builder().id(new Long(1)).build();
MyObject obj2 = MyObject.builder().id(new Long(2)).build();
MyObject obj6 = MyObject.builder().id(new Long(6)).build();
MyObject obj7 = MyObject.builder().id(new Long(7)).build();
return Stream.of(obj1, obj2, obj6, obj7).collect(Collectors.toCollection(LinkedHashSet::new));
}
}
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Getter
@Builder
@ToString
class MyObject {
@EqualsAndHashCode.Include
private Long id;
private Boolean isLatest;
}
输出:

MyObject (id = 5, isLatest = true)

MyObject (id = 6, isLatest = null)

MyObject (id = 7, isLatest = null)

MyObject (id = 1, isLatest = true)

MyObject (id = 2, isLatest = false)

MyObject (id = 3, isLatest = true)

MyObject (id = 4, isLatest = true)

你可以试试这个。对于您的示例,

可以正常工作。
BinaryOperator<MyObject> mergeFunction = (old, lst) -> {
if (lst.getLatest() != null && lst.getLatest()) {return lst;}
else if (old.getLatest() == null && lst.getLatest() == null) {return lst;}
else if (old.getLatest() == null && lst.getLatest() != null && !lst.getLatest()) {return lst;}
else {return old;}
};
List<MyObject> collect = new ArrayList<>(Stream.concat(listWithIsLatestSet.stream(), listWithIsLatestNull.stream())
.collect(Collectors.toMap(MyObject::getId, Function.identity(), mergeFunction))
.values());

相关内容

  • 没有找到相关文章

最新更新