【发布时间】:2021-01-29 15:38:10
【问题描述】:
我想从一个列表中排除对象,其 id 包含在另一个 Id 列表中,并获得一个字段名称字符串。" Care, Safe"
例子:
public interface Selectable {
Long getId();
String getName();
}
我有一个list<Selectable> tenants
id, name
34, "House"
35, "Care"
36, "Villa"
37, "Safe"
还有一个List<Long> idTenantsWithAlerts
id
34
36
我需要结果字符串 tenantsNoAlerts 为 "Care, Safe"
我对 java8 流没有太多经验,所以我尝试了相同的过滤器、映射和归约......但没有运气。
【问题讨论】:
-
List<Long, String>?该类型不存在。 -
@Polygnome。不,我会设置代码
-
@Eklavya 我设置了一些代码...
-
是否可以将
tenants用作Map<Long, String>而不是List<Selectable>?如果可能,那么您的流过滤器会更有效、更简单。 -
您可以将
idTenantsWithAlertsList替换为Set。它最常用的实现HashSet的查找时间复杂度为 O(1)。
标签: java dictionary filter java-stream reduce