如果我有一个值"foo",以及一个ftw.containsValue("foo")返回true的HashMap<String> ftw,我怎么能得到对应的键呢?我必须遍历hashmap吗?做到这一点的最好方法是什么?
发布于 2021-05-30 18:45:23
从Map中获取给定值的键的最简单实用方法:
public static void fetchValue(Map<String, Integer> map, Integer i)
{
Stream stream = map.entrySet().stream().filter(val-> val.getValue().equals(i)).map(Map.Entry::getKey);
stream.forEach(System.out::println);
}详细说明:
然后我们使用
最后,我们使用forEach(
https://stackoverflow.com/questions/1383797
复制相似问题