简体   繁体   English

如何在 Map 中获取给定值的密钥<string, list<string> &gt;?</string,>

[英]How can I get the a key given a value in a Map<String, List<String>>?

Given a Map with the following structures {group2=[form4], group1=[form1, form2, form3]} how can I get the key(groupN) based on a value(formN)?给定具有以下结构的 Map {group2=[form4], group1=[form1, form2, form3]}如何根据值(formN)获取密钥(groupN)? Basically if one of the entries has a match with my input form I want to get that entry's key to set as the new form.基本上,如果其中一个条目与我的输入表单匹配,我希望将该条目的键设置为新表单。

I was thinking of something like this:我在想这样的事情:

newForm= formAliasList.entrySet()
        .stream()
        .filter(entry -> form.toLowerCase().equals(entry.getValue()))
        .map(Map.Entry::getKey)
        .findFirst().get();

Since the value in Map<String, List<String>> is List<String> , you can simply check the formN existed in List using contains, and then collect the key of entry由于Map<String, List<String>>中的值为List<String> ,你可以简单地使用 contains 来检查List中存在的formN ,然后收集 entry 的 key

newForm = formAliasList.entrySet()
                      .stream()
                      .filter(entry -> entry.getValue().contains(form.toLowerCase()))
                      .findFirst().map(Map.Entry::getKey).orElse(null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在map.key中包含字符串值? - How can I contains String value with map.key? 如何检查Map中的键是否以给定的String值开头 - How to check if a key in a Map starts with a given String value 如何转换地图 <String, List<String> &gt;按键值放入文件 - How to transform Map<String, List<String>> into Files by Key Value 如何从Map中获取值 <String List<Object> &gt; - How can I get the values from Map<String List<Object>> JPQL-如何在实体中搜索键和值(Hibernate OGM(MongoDB)-地图 <String, String> ) - JPQL - How can i search entities for key and value (Hibernate OGM (MongoDB) - Map<String, String>) 转换列表<String>到地图<String, List<String> &gt; 以列表值为映射键,以空列表为映射值 java 8 - Convert List<String> to Map<String, List<String>> with list value as map key and map value with empty list java 8 如何将名称作为列表获取<String>或设置<String>鉴于枚举? - How can I get the names as a List<String> or Set<String> given the Enums? 无法获得地图的价值 <BigDecimal, String> 按键 - Can't get value of Map<BigDecimal, String> by key 比较两张地图<String, List<String> &gt; 并获取地图键 + 地图值作为差异 - Compare two Maps<String, List<String>> and get map key + map value as differences 如何从列表中删除键/值对<Map<String, Object> &gt; - How to remove the key/value pair from a List<Map<String, Object>>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM