如何在特定属性出现多次的情况下从链表中移除项?

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

How can I remove items from a LinkedList if a certain property appears several times?

问题

我有一个LinkedList其中装满了类型为WebCacheEvents的对象每个对象都有诸如descriptioneventlabellectureId等属性

// 用之前收到的数据填充列表
List<WebCacheEvents> result = new LinkedList<WebCacheEvents>();
for (WebCache event : events) 
    result.add(new thabella.dto.out.WebCacheEvents(event));
return result;

我想要做的是删除具有已被列表中其他WebCacheEvent使用的lectureId的任何WebCacheEvent,以便在我的结果中每个lectureId只出现一次。

因此,我不能简单地使用

if (!result.contains(event))
    result.add(event);

因为我实际上并不是在寻找真正的重复项,即WebCacheEvent的每个属性具有相同的值,而只是在寻找具有相同lectureId的对象。在我收到的事件中可以有两个或更多具有相同lectureId的对象。

是否有一种类似于“contains”方法的方式,但只针对对象的某些属性呢?


<details>
<summary>英文:</summary>

I have a LinkedList that is filled with objects of the type WebCacheEvents. Each object has attributes like description, event, label, lectureId, etc: 

//filling the list with data received earlier
List<WebCacheEvents> result = new LinkedList<WebCacheEvents>();
for (WebCache event : events)
result.add(new thabella.dto.out.WebCacheEvents(event));
return result;

What I want to do is to remove any WebCacheEvent that has a lectureId that is already used by another WebCacheEvent in the list - so that in my result every lectureId only appears once. 

Therefore, I can&#39;t simply use 

if(!result.contains(event))
result.add(event);

because I am not really looking for real duplicates, where every attribute of the WebCacheEvent has the same value, but only for objects with the same lectureId. There can be two or more objects with the same lectureId in my received events. 

Is there a similar way to use the &quot;contains&quot;-method but only for certain properties of an object? 


</details>


# 答案1
**得分**: 2

你可以简单地使用一个筛选器:

```java
List<WebCacheEvents> result = new LinkedList<WebCacheEvents>();
for (WebCache event : events)
    if (result.stream().noneMatch(w -> w.getLectureId().equals(event.getLectureId())))
        result.add(new thabella.dto.out.WebCacheEvents(event));
return result;

我假设 lectureId 不会为空。

英文:

you could use simply a filter:

List&lt;WebCacheEvents&gt; result = new LinkedList&lt;WebCacheEvents&gt;();
    for (WebCache event : events)
        if (result.stream().noneMatch(w -&gt; w.getLectureId().equals(event.getLectureId())))
            result.add(new thabella.dto.out.WebCacheEvents(event));
    return result;

I suppose lecturId cannot be null.

答案2

得分: 0

最简单的方法是:

LinkedList<WebCacheEvents> result = new LinkedList<WebCacheEvents>();
if (!doesContain(result, event))
    result.add(event); 

public boolean doesContain(LinkedList<Integer> result, WebCacheEvents obj) {
    boolean isContained = false;
    for (WebCacheEvents data : result){
        if (data.getLectureId() == obj.getLectureId()){
            isContained = true;
            break;
        }
    }
    return isContained;
}
英文:

The simplest way is:

LinkedList&lt;WebCacheEvents&gt; result = new LinkedList&lt;WebCacheEvents&gt;();
if(!doesContain(result, event))
result.add(event); 

public boolean doesContain(LinkedList&lt;Integer&gt; result, WebCacheEvents obj) {
  boolean isContained = false;
  for (WebCacheEvents data : result){
    if (data.getLectureId() == obj.getLectureId()){
      isContained = true;
      break;
    }
  }
  return isContained;
}

huangapple
  • 本文由 发表于 2020年9月9日 20:34:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63811823.html
匿名

发表评论

匿名网友

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

确定