java - 如果某个属性出现多次,如何从 LinkedList 中删除项目?
问题描述
我有一个 LinkedList,其中填充了 WebCacheEvents 类型的对象。每个对象都有描述、事件、标签、lectureId 等属性:
//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;
我想要做的是删除任何具有已被列表中另一个 WebCacheEvent 使用的演讲 ID 的 WebCacheEvent - 这样在我的结果中每个演讲 ID 只出现一次。
因此,我不能简单地使用
if(!result.contains(event))
result.add(event);
因为我并不是真的在寻找真正的重复项,其中 WebCacheEvent 的每个属性都具有相同的值,但仅适用于具有相同 LectureId 的对象。在我收到的事件中,可以有两个或多个具有相同的 LectureId 的对象。
是否有类似的方法可以使用“包含”方法,但仅适用于对象的某些属性?
解决方案
你可以简单地使用一个过滤器:
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;
我想 lecturId 不能为空。
推荐阅读
- python - Pandas DataFrame:A列窗口中B列值的平均值
- html - 用角度编写 require() 的测试用例
- swift - 由于赋值语句的错位,函数总是返回 nil
- python - Django 'NoneType' 对象没有属性 'filter'
- python - 是否可以将操作分配给操作中使用的同一变量?
- excel - 是否可以创建一个宏来提取选定单元格中的文本,将其添加到文件路径,然后使单元格成为指向指定文件的超链接?
- ubuntu - 如何在 ubuntu 上安装 Tensorflow-GPU
- deployment - Hyperledger Fabric 中的节点在实际场景中部署在哪里?
- php - Symfony:在 live/prod 服务器上不起作用“RedirectResponse”
- angular - 如何为多个下拉菜单绑定选定的选项值?