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

huangapple go评论73阅读模式
英文:

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 编码的特殊字符(如 &quot;),这些字符在代码中并不是合法的 Java 语法。在实际代码中,你应该使用原始的 JSON 或 Java 字符串表示方式。以上代码假设你已经解析了这些 JSON 字符串并将其转换为了 ManageEventDTO 对象的列表。如果你需要进一步的帮助,请提供更多上下文信息。

英文:
List&lt;ManageEventDTO&gt; yourUpcomingEventsList = eventsDao.findYourUpcomingEvents(members, LocalDate.now());
List&lt;ManageEventDTO&gt; 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

[
    {
      &quot;id&quot;: 339,
      &quot;recurrenceEventId&quot;: null,
      &quot;eventName&quot;: &quot;Magic Moments&quot;,
      &quot;passport&quot;: true
    },
    {
      &quot;id&quot;: 154,
      &quot;recurrenceEventId&quot;: null,
      &quot;eventName&quot;: &quot;Women&#39;s Networking League Lunch&quot;,
      &quot;passport&quot;: true
    },
    {
      &quot;id&quot;: 155,
      &quot;recurrenceEventId&quot;: null,
      &quot;eventName&quot;: &quot;Gingerbread&quot;,
      &quot;passport&quot;: false
    },
    {
      &quot;id&quot;: 156,
      &quot;recurrenceEventId&quot;: null,
      &quot;eventName&quot;: &quot;Business Network&quot;,
      &quot;passport&quot;: false
    },
    {
      &quot;id&quot;: 163,
      &quot;recurrenceEventId&quot;: null,
      &quot;eventName&quot;: &quot;Gingerbread&quot;,
      &quot;passport&quot;: false
    },
    {
      &quot;id&quot;: 164,
      &quot;recurrenceEventId&quot;: null,
      &quot;eventName&quot;: &quot;Women&#39;s Networking League Lunch&quot;,
      &quot;passport&quot;: false
    }
]

2.yourUpcomingRecurrenceEvents

[
    {
      &quot;id&quot;: 339,
      &quot;recurrenceEventId&quot;: 13,
      &quot;eventName&quot;: &quot;Magic Moments&quot;,
      &quot;passport&quot;: true
    },
    {
      &quot;id&quot;: 339,
      &quot;recurrenceEventId&quot;: 14,
      &quot;eventName&quot;: &quot;Magic Moments&quot;,
      &quot;passport&quot;: true
    },
    {
      &quot;id&quot;: 336,
      &quot;recurrenceEventId&quot;: 6,
      &quot;eventName&quot;: &quot;Networking Cigars &amp; Drink&quot;,
      &quot;passport&quot;: true
    },
    {
      &quot;id&quot;: 336,
      &quot;recurrenceEventId&quot;: 7,
      &quot;eventName&quot;: &quot;Networking Cigars &amp; Drink&quot;,
      &quot;passport&quot;: true
    },
    {
      &quot;id&quot;: 335,
      &quot;recurrenceEventId&quot;: 3,
      &quot;eventName&quot;: &quot;Party with Dance&quot;,
      &quot;passport&quot;: true
    },
    {
      &quot;id&quot;: 335,
      &quot;recurrenceEventId&quot;: 4,
      &quot;eventName&quot;: &quot;Party with Dance&quot;,
      &quot;passport&quot;: true
    },
    {
      &quot;id&quot;: 337,
      &quot;recurrenceEventId&quot;: 8,
      &quot;eventName&quot;: &quot;Monday MasterMind&quot;,
      &quot;passport&quot;: true
    },
    {
      &quot;id&quot;: 338,
      &quot;recurrenceEventId&quot;: 11,
      &quot;eventName&quot;: &quot;Drink &amp; smoke only for men&quot;,
      &quot;passport&quot;: true
    }
  ]    
  1. 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&lt;Integer&gt; eventIds = yourUpcomingRecurrenceEvents.stream()
                                                    .map(e -&gt; e.getId())
                                                    .collect(Collectors.toSet());
List&lt;ManageEventDTO&gt; newEvents = yourUpcomingEventsList.stream()
                                          .filter(e -&gt; !eventIds.contains(e.getId()))
                                          .collect(Collectors.toList());
yourUpcomingRecurrenceEvents.addAll(newEvents);                      
                            .

huangapple
  • 本文由 发表于 2020年10月1日 14:21:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/64150062.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定