我想从ID包含在另一个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
我需要结果String tenantsNoAlerts为"Care, Safe"
我对java8流没有太多经验,所以我尝试了相同的过滤器,映射和归约...但是没有运气。
您可以通过过滤列表Id
,然后map
,再collect
以加盟收藏家,以减少的Stream
一个String
。
List<Selectable> tenants;
String tenantsNoAlerts = tenants.stream()
.filter(entry -> !idTenantsWithAlerts.contains(entry.getId()))
.map(entry -> entry.getName())
.collect(Collectors.joining(", "));
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句