java - 获取 map 键而不是 map 值

标签 java

<分区>

我有一些这样的数据:

Map<Integer, String> foo

现在我可以用foo.get(1)获取对应的String

但是否也有可能获得所有具有 String "abc"的 Integer

像这样的伪代码:Integer[] someMore = foo.getKeys("abc")

最佳答案

尝试:

Set<Integer> myInts = new HashSet<Integer>();
for(Entry<Integer, String> entry : foo.entrySet()) { // go through the entries
    if(entry.getValue().equals("abc")) {             // check the value
        myInts.add(entry.getKey());                  // add the key
    }
}
// myInts now contains all the keys for which the value equals "abc"

关于java - 获取 map 键而不是 map 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16734420/

相关文章:

java - ThreadLocal 中的多个对象

java - 这个 NullPointerException 是什么意思?

java - native 操作系统外观 Netbeans 不能与 UIManager.setLookAndFeel(nativeLF) 一起使用;

java - 是什么导致了 java.lang.ArrayIndexOutOfBoundsException 以及如何防止它?

Java Serialized - 序列化/反序列化是否安全?

java - Infinispan 大小驱逐 + diskStore 溢出

java - 如何通过循环连续调用方法?

java - Gradle (java) : test task should use generated . jar 而不是类路径中的 .class 文件

java - 在java中的Jframe上绘制简单的矩形

Java-设置 datetime17 :30 if the current time is before 17:00