gpt4 book ai didi

Java比较对象列表以查找重复项并将它们放入新列表中

转载 作者:行者123 更新时间:2023-12-02 06:16:06 25 4
gpt4 key购买 nike

当比较同一对象的两个列表以查找它们的重复位置时,两个列表都为空。

List<Appointment> appointmentByName = sM.getAppointmentByName(first.getText(),last.getText());
List<Appointment>appointmentByDate=sM.getAppointmentByDate(LocalDate.now());System.out.println(appointmentByName);
//Populating the lists (This works)


List<Appointment> common = appointmentByDate.stream().filter(appointmentByName::contains).collect(toList());

填充列表后,我想找到相同的约会对象并将它们放入新列表中,例如

List1 [Appointment@20, Appointment@c3, Appointment@d9, Appointment@175]
List2 [Appointment@23, Appointment@175]
List3 [Appointment@175]

等于 Appointment 类中的覆盖

public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Appointment)) return false;
Appointment that = (Appointment) o;
return id == that.id;
}

但是通用列表是空的,我们将不胜感激。

最佳答案

不确定我是否正确理解你的问题,但在我看来,你试图在这里做两个列表的交集。如果是,您可以使用 Collection 的 keepAll() 方法来完成:

list1.retainAll(list2)

list1 现在将仅包含两个列表的重复项

关于Java比较对象列表以查找重复项并将它们放入新列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55870919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com