[英]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.