英文:
How can I remove items from a LinkedList if a certain property appears several times?
问题
我有一个LinkedList,其中装满了类型为WebCacheEvents的对象。每个对象都有诸如description、event、label、lectureId等属性:
// 用之前收到的数据填充列表
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'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 "contains"-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<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;
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<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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论