[英]Java Using Multiple predicate for anyMatch
I am having a List
of HashMap
ie List<Map<String, Object>> result
.我有一个
HashMap
List
,即List<Map<String, Object>> result
。 I want to check if some key value is present on this list or not.我想检查此列表中是否存在某个键值。
For Example:例如:
Name Count
Marvel 10
DC 0
I want that my list of map should contain at least one entry for the above table for that I am using anyMatch我希望我的地图列表至少包含上表的一个条目,因为我正在使用anyMatch
Assert.assertTrue(
result.stream().anyMatch(
ag -> ( "Marvel".equals(ag.get("Name")) && 10==(int)ag.get("Count"))
));
How can I use multiple Predicates ?如何使用多个谓词? I can have a list of 1000
HashMap
, I just want to check if any two Hash-map from my list contains those two entries.我可以有一个 1000
HashMap
的列表,我只想检查列表中的任何两个 Hash-map 是否包含这两个条目。
Your data structure seems wrong for a start.您的数据结构一开始似乎是错误的。 A
List<Map<String, Object>>
, where each map represents a row and has only one key and one value?一个
List<Map<String, Object>>
,其中每个映射代表一行并且只有一个键和一个值? You just need a Map<String, Integer>
.你只需要一个
Map<String, Integer>
。 The string is the name, the integer is the count.字符串是名称,整数是计数。 Remember to give it a meaningful name, eg
comicUniverseToSuperheroFrequency
.记住给它一个有意义的名字,例如
comicUniverseToSuperheroFrequency
。
With that said, Predicate
has an and
method which you can use to chain conditions together.话虽如此,
Predicate
有一个and
方法,您可以使用它来将条件链接在一起。 It might look something like this:它可能看起来像这样:
public static void main(String[] args)
{
Map<String, Integer> comicUniverseToSuperheroFrequency = /*something*/;
boolean isMarvelCountTen = comicUniverseToSuperheroFrequency.entrySet().stream()
.anyMatch(row -> isMarvel().and(isTen()).test(row));
}
private static Predicate<Map.Entry<String, Integer>> isMarvel()
{
return row -> "Marvel".equals(row.getKey());
}
private static Predicate<Map.Entry<String, Integer>> isTen()
{
return row -> row.getValue() == 10;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.