static final List<String> allowedType = asList(type1,type2);
Set<String> set1 = serviceClass.getType()
.stream()
.filter(type -> allowedType.contains(type))
.collect(toSet());需求由两部分组成,
part1。如果从serviceClass接收的类型具有allowedType中提到的任何服务类型,则将其添加到set1。
第二部分.After上面的操作,我需要检查set1是否包含两种类型,如果包含,则需要删除其中一种类型。
Part1很简单,到目前为止,我已经用part2实现了
if (set1.contains("type1" && set1.contains("typep2") {
set1.remove("type");
}有没有什么办法可以将part1和2合并到一个操作中?
谢谢,
发布于 2019-03-28 05:07:39
如果总是有两种类型,并且您始终最多只能保留其中一种,则可以使用limit
Set<String> set1 = serviceClass.getType()
.stream()
.filter(type -> allowedType.contains(type))
.limit(1)
.collect(toSet());如果至少有一个元素通过筛选器,则limit(1)将返回一个包含1个元素的流,否则返回空流。在我看来,这将给你你想要的(未测试的)。
https://stackoverflow.com/questions/55386177
复制相似问题