如果存在特定键 Java 8 的值,则检查列表映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50717771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:46:47  来源:igfitidea点击:

Check in Map of list if exists value for specific key Java 8

javalistdictionaryjava-8java-stream

提问by Marinescu Raluca

In Java 7 I have

在 Java 7 中,我有

Map<String, List<String>> m = new HashMap<String, List<String>>();
boolean result = false;
m.put("Name1", Arrays.asList("abc*1"));
m.put("Name2", Arrays.asList("abc@*1"));


for (Map.Entry<String, List<String>> me : m.entrySet()) {
    String key = me.getKey();
    List<String> valueList = me.getValue();
    if (key.equals("Name2"){
        System.out.print("Values: ");
        for (String s : valueList) {
            if(s.contains("@"){
                result = true;
            }
        }
    }
} 

How can I get ?n a bool result for Name2if it contains @using any match?

Name2如果它包含@使用任何匹配项,我如何获得 ?na bool 结果?

I tried using The following Code but I Don t know how to use IT for specific key

我尝试使用以下代码,但我不知道如何将 IT 用于特定密钥

result = m.values().stream().anyMatch(v -> v.contains("@"))

采纳答案by Michael

You can simply use m.get("Name2"), place the (nullable) result into an Optionaland then use a mapping:

您可以简单地使用m.get("Name2"),将(可为空的)结果放入 anOptional然后使用映射:

boolean result = Optional.ofNullable(m.get("Name2"))
    .map(l -> l.stream().anyMatch(s -> s.contains("@")))
    .orElse(false);

This is preferable to looping over the entry set, as HashMap.getis O(1) and iterating over the entry set is O(n).

这比遍历条目集更可取,因为HashMap.getO(1) 和迭代条目集是 O(n)。

回答by Ousmane D.

create a stream from the entrySet()and then provide your criteria in the anyMatch:

从 中创建一个流entrySet(),然后在 中提供您的条件anyMatch

result = m.entrySet()
          .stream()
          .anyMatch(v -> Objects.equals("Name2", v.getKey()) && 
               v.getValue().stream().anyMatch(s -> s.contains("@")));

or using getOrDefault:

或使用getOrDefault

result = m.getOrDefault("Name2", Collections.emptyList())
          .stream()
          .anyMatch(s -> s.contains("@"));

回答by Vinay Prajapati

Just add correct filter condition:

只需添加正确的过滤条件:

m.entrySet()
.stream()
.anyMatch(entry-> entry.getKey().equals(Name2) && 
   entry.getValue()
.stream()
.anyMatch(string -> string.contains("@"))
.getValue();

回答by Eran

First you should filter by the required key, then you can use anyMatchto determine if the value of that key contains an element with a '@' character:

首先,您应该按所需的键进行过滤,然后您可以使用它anyMatch来确定该键的值是否包含带有“@”字符的元素:

result = m.entrySet ()
          .stream ()
          .filter (e->e.getKey ().equals (Name2))
          .anyMatch (e->e.getValue ().stream ().anyMatch (s->s.contains ("@")));

回答by YCF_L

What about

关于什么

String name = "Name1";
boolean result= m.containsKey(name) && m.get(name).stream().anyMatch(a -> a.contains("@"));

回答by Nikolas

Do the following:

请执行下列操作:

boolean result = m.getOrDefault("Name2", Collections.emptyList()).stream()
    .anyMatch(i -> i.contains("@"));

If the Mapcontains a correct key, check whether any of its element of the Listas value contains the particular character. If the Mapdoesn't contain the key, mock the empty Collectionwhich doesn't contain anything at all and the result is evaluated automatically as false.

如果Map包含正确的键,则检查Listas 值的任何元素是否包含特定字符。如果Map不包含键,则模拟Collection根本不包含任何内容的空,结果将自动评估为false

Edit: As @Michael suggested, using the Collections.emptyList()is a better choice than new ArrayList<>().

编辑:正如@Michael 所建议的,使用Collections.emptyList()是比new ArrayList<>().

回答by Bohemian

Try this:

尝试这个:

boolean result = m.entrySet().stream()
    .filter(e -> e.getKey().equals(Name2))
    .map(Map.Entry::getValue)
    .flatMap(List::stream)
    .anyMatch(s -> s.contains("@"));