[英]List<Map<String, String>> contains all elements from String[]. How to check it?
I need to check that List<Map<String, String>>
contains all elements from String[]
.我需要检查
List<Map<String, String>>
包含来自String[]
所有元素。
I solved this problem like this:我是这样解决这个问题的:
private boolean method(List<Map<String, String>> listWithMaps, String[] conditions){
List<String> list = new ArrayList<>();
for (Map<String, String> mapFromList : listWithMaps) {
list.add(mapFromList.get("CAUSE"));
}
return list.containsAll(Arrays.asList(conditions));
}
But maybe there is a more correct way to solve my problem?但也许有更正确的方法来解决我的问题?
A couple of possibilities with streams:流的几种可能性:
listWithMaps.stream().map(map -> map.get("CAUSE")).collect(toSet()).containsAll(Arrays.asList(conditions));
and和
Arrays.stream(conditions)
.allMatch(condition -> listWithMaps.stream().anyMatch(map -> map.get("CAUSE").equals(condition)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.