英文:
Compare two list of property of id and remove only that object from list1 not remove from list2 then merge into single list in java
问题
List<ManageEventDTO> yourUpcomingEventsList = eventsDao.findYourUpcomingEvents(members, LocalDate.now());
List<ManageEventDTO> yourUpcomingRecurrenceEvents = eventsDao.findYourUpcomingRecurrenceEvents(members, LocalDate.now());
// Removing duplicates from yourUpcomingEventsList based on id property
Set<Long> eventIdsToRemove = new HashSet<>();
List<ManageEventDTO> distinctYourUpcomingEvents = new ArrayList<>();
for (ManageEventDTO upcomingEvent : yourUpcomingEventsList) {
if (!eventIdsToRemove.contains(upcomingEvent.getId())) {
eventIdsToRemove.add(upcomingEvent.getId());
distinctYourUpcomingEvents.add(upcomingEvent);
}
}
// Merging distinctYourUpcomingEvents with yourUpcomingRecurrenceEvents
List<ManageEventDTO> mergedEventsList = new ArrayList<>(distinctYourUpcomingEvents);
mergedEventsList.addAll(yourUpcomingRecurrenceEvents);
注:请注意,你提供的代码部分中包含了 HTML 编码的特殊字符(如 "
),这些字符在代码中并不是合法的 Java 语法。在实际代码中,你应该使用原始的 JSON 或 Java 字符串表示方式。以上代码假设你已经解析了这些 JSON 字符串并将其转换为了 ManageEventDTO
对象的列表。如果你需要进一步的帮助,请提供更多上下文信息。
英文:
List<ManageEventDTO> yourUpcomingEventsList = eventsDao.findYourUpcomingEvents(members, LocalDate.now());
List<ManageEventDTO> yourUpcomingRecurrenceEvents = eventsDao.findYourUpcomingRecurrenceEvents(members, LocalDate.now());
How to Compare two DTO list based on id property and remove only that object from this yourUpcomingEventsList and it will not remove from yourUpcomingRecurrenceEvents. In this list duplicate is required.
After the removed duplicate object from yourUpcomingEventsList i want merge this list with yourUpcomingRecurrenceEvents and create new List.
1.yourUpcomingEventsList
[
{
"id": 339,
"recurrenceEventId": null,
"eventName": "Magic Moments",
"passport": true
},
{
"id": 154,
"recurrenceEventId": null,
"eventName": "Women's Networking League Lunch",
"passport": true
},
{
"id": 155,
"recurrenceEventId": null,
"eventName": "Gingerbread",
"passport": false
},
{
"id": 156,
"recurrenceEventId": null,
"eventName": "Business Network",
"passport": false
},
{
"id": 163,
"recurrenceEventId": null,
"eventName": "Gingerbread",
"passport": false
},
{
"id": 164,
"recurrenceEventId": null,
"eventName": "Women's Networking League Lunch",
"passport": false
}
]
2.yourUpcomingRecurrenceEvents
[
{
"id": 339,
"recurrenceEventId": 13,
"eventName": "Magic Moments",
"passport": true
},
{
"id": 339,
"recurrenceEventId": 14,
"eventName": "Magic Moments",
"passport": true
},
{
"id": 336,
"recurrenceEventId": 6,
"eventName": "Networking Cigars & Drink",
"passport": true
},
{
"id": 336,
"recurrenceEventId": 7,
"eventName": "Networking Cigars & Drink",
"passport": true
},
{
"id": 335,
"recurrenceEventId": 3,
"eventName": "Party with Dance",
"passport": true
},
{
"id": 335,
"recurrenceEventId": 4,
"eventName": "Party with Dance",
"passport": true
},
{
"id": 337,
"recurrenceEventId": 8,
"eventName": "Monday MasterMind",
"passport": true
},
{
"id": 338,
"recurrenceEventId": 11,
"eventName": "Drink & smoke only for men",
"passport": true
}
]
- I have try
for (ManageEventDTO yourUpcomingRecurrence : yourUpcomingRecurrenceEvents) {
if (yourUpcomingEvents.contains(yourUpcomingRecurrence.getId())) {
yourUpcomingEvents.remove(yourUpcomingRecurrence);
}
}
答案1
得分: 1
创建一个由yourUpcomingRecurrenceEvents
中的id
组成的集合,然后从yourUpcomingEventsList
列表中过滤掉那些在集合中不存在的元素,并将它们收集到一个新的列表中。然后将新的列表添加到yourUpcomingRecurrenceEvents
中。
Set<Integer> eventIds = yourUpcomingRecurrenceEvents.stream()
.map(e -> e.getId())
.collect(Collectors.toSet());
List<ManageEventDTO> newEvents = yourUpcomingEventsList.stream()
.filter(e -> !eventIds.contains(e.getId()))
.collect(Collectors.toList());
yourUpcomingRecurrenceEvents.addAll(newEvents);
英文:
Create a set of id
's of yourUpcomingRecurrenceEvents
then filter yourUpcomingEventsList
list those do not exist in set and collect in new list. Then add new list into yourUpcomingRecurrenceEvents
Set<Integer> eventIds = yourUpcomingRecurrenceEvents.stream()
.map(e -> e.getId())
.collect(Collectors.toSet());
List<ManageEventDTO> newEvents = yourUpcomingEventsList.stream()
.filter(e -> !eventIds.contains(e.getId()))
.collect(Collectors.toList());
yourUpcomingRecurrenceEvents.addAll(newEvents);
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论