我有一个方法需要 List<String> locations
作为参数。我正在根据特定条件以相同的方法填充用户列表。现在我想要的是,用户应该出现在这个位置。简而言之,如果用户的位置出现在所提供的位置列表中,则该用户是有效的。地点。
这是我当前的代码:
primaryList.stream()
.filter(some_pattern_matching)[.MATCH THE LOCATION HERE]
.map(user -> user.getNames())
.collect(toList())
有没有办法说locations.contains(user -> user.getLocation())
或者
user -> user.getLocation().isPresentIn(locations)
或者将位置转换为一个流然后进行匹配?
这是我当前的代码:
.filter(locations.stream().filter(loc -> loc.equalsIgnoreCase(s.getLocation())))
当然不能编译。
最佳答案
你的意思是:
.filter(user -> locations.contains(user.getLocation())
关于用于查找 "contains"的 Java 8 lambda 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43890986/