简体   繁体   English

Java Hashmap:如何从值中获取键?

[英]Java Hashmap: How to get key from value?

If I have the value "foo" , and a HashMap<String> ftw for which ftw.containsValue("foo") returns true , how can I get the corresponding key?如果我有值"foo"和一个HashMap<String> ftw ftw.containsValue("foo")返回true ,我怎样才能获得相应的键? Do I have to loop through the hashmap?我必须遍历哈希图吗? What is the best way to do that?最好的方法是什么?

If your data structure has many-to-one mapping between keys and values you should iterate over entries and pick all suitable keys:如果您的数据结构在键和值之间具有多对一映射,您应该迭代条目并选择所有合适的键:

public static <T, E> Set<T> getKeysByValue(Map<T, E> map, E value) {
    Set<T> keys = new HashSet<T>();
    for (Entry<T, E> entry : map.entrySet()) {
        if (Objects.equals(value, entry.getValue())) {
            keys.add(entry.getKey());
        }
    }
    return keys;
}

In case of one-to-one relationship, you can return the first matched key:一对一关系的情况下,您可以返回第一个匹配的键:

public static <T, E> T getKeyByValue(Map<T, E> map, E value) {
    for (Entry<T, E> entry : map.entrySet()) {
        if (Objects.equals(value, entry.getValue())) {
            return entry.getKey();
        }
    }
    return null;
}

In Java 8:在 Java 8 中:

public static <T, E> Set<T> getKeysByValue(Map<T, E> map, E value) {
    return map.entrySet()
              .stream()
              .filter(entry -> Objects.equals(entry.getValue(), value))
              .map(Map.Entry::getKey)
              .collect(Collectors.toSet());
}

Also, for Guava users, BiMap may be useful.此外,对于 Guava 用户, BiMap可能很有用。 For example:例如:

BiMap<Token, Character> tokenToChar = 
    ImmutableBiMap.of(Token.LEFT_BRACKET, '[', Token.LEFT_PARENTHESIS, '(');
Token token = tokenToChar.inverse().get('(');
Character c = tokenToChar.get(token);

If you choose to use the Commons Collections library instead of the standard Java Collections framework, you can achieve this with ease.如果您选择使用Commons Collections 库而不是标准的 Java Collections 框架,则可以轻松实现这一点。

The BidiMap interface in the Collections library is a bi-directional map, allowing you to map a key to a value (like normal maps), and also to map a value to a key, thus allowing you to perform lookups in both directions. Collections 库中的BidiMap接口是一个双向映射,允许您将键映射到值(如法线映射),也可以将值映射到键,从而允许您在两个方向上执行查找。 Obtaining a key for a value is supported by the getKey() method. getKey()方法支持获取值的键。

There is a caveat though, bidi maps cannot have multiple values mapped to keys, and hence unless your data set has 1:1 mappings between keys and values, you cannot use bidi maps.但是有一个警告,双向映射不能将多个值映射到键,因此除非您的数据集在键和值之间具有 1:1 映射,否则您不能使用双向映射。


If you want to rely on the Java Collections API, you will have to ensure the 1:1 relationship between keys and values at the time of inserting the value into the map.如果要依赖 Java Collections API,则必须在将值插入映射时确保键和值之间的 1:1 关系。 This is easier said than done.这说起来容易做起来难。

Once you can ensure that, use the entrySet() method to obtain the set of entries (mappings) in the Map.一旦可以确保,使用entrySet()方法获取 Map 中的条目集(映射)。 Once you have obtained the set whose type is Map.Entry , iterate through the entries, comparing the stored value against the expected, and obtain the corresponding key .获得类型为Map.Entry的集合后,遍历条目,将存储的值与预期进行比较,并获得相应的 key


Support for bidi maps with generics can be found in Google Guava and the refactored Commons-Collections libraries (the latter is not an Apache project).可以在Google Guava和重构的Commons-Collections库(后者不是 Apache 项目)中找到对具有泛型的双向映射的支持。 Thanks to Esko for pointing out the missing generic support in Apache Commons Collections.感谢 Esko 指出 Apache Commons Collections 中缺少的通用支持。 Using collections with generics makes more maintainable code.使用带有泛型的集合可以使代码更易于维护。


Since version 4.0 the official Apache Commons Collections™ library supports generics .4.0 版开始,官方 Apache Commons Collections™ 库支持泛型

See the summary page of the " org.apache.commons.collections4.bidimap " package for the list of available implementations of the BidiMap , OrderedBidiMap and SortedBidiMap interfaces that now support Java generics .有关现在支持 Java泛型BidiMapOrderedBidiMapSortedBidiMap接口的可用实现列表,请参阅“ org.apache.commons.collections4.bidimap ”包的摘要页面。

public class NewClass1 {

    public static void main(String[] args) {
       Map<Integer, String> testMap = new HashMap<Integer, String>();
        testMap.put(10, "a");
        testMap.put(20, "b");
        testMap.put(30, "c");
        testMap.put(40, "d");
        for (Entry<Integer, String> entry : testMap.entrySet()) {
            if (entry.getValue().equals("c")) {
                System.out.println(entry.getKey());
            }
        }
    }
}

Some additional info... May be useful to you一些额外的信息......可能对你有用

Above method may not be good if your hashmap is really big.如果您的哈希图非常大,则上述方法可能并不好。 If your hashmap contain unique key to unique value mapping, you can maintain one more hashmap that contain mapping from Value to Key.如果您的哈希图包含唯一键到唯一值的映射,您可以再维护一个包含从值到键的映射的哈希图。

That is you have to maintain two hashmaps那就是你必须维护两个哈希图

1. Key to value

2. Value to key 

In that case you can use second hashmap to get key.在这种情况下,您可以使用第二个哈希图来获取密钥。

You could insert both the key,value pair and its inverse into your map structure您可以将键值对及其逆插入到您的地图结构中

map.put("theKey", "theValue");
map.put("theValue", "theKey");

Using map.get("theValue") will then return "theKey".然后使用 map.get("theValue") 将返回 "theKey"。

It's a quick and dirty way that I've made constant maps, which will only work for a select few datasets:这是我制作常量映射的一种快速而肮脏的方式,它仅适用于选定的几个数据集:

  • Contains only 1 to 1 pairs仅包含 1 到 1 对
  • Set of values is disjoint from the set of keys (1->2, 2->3 breaks it)值集与键集不相交(1->2, 2->3 打破它)

I think your choices are我认为你的选择是

  • Use a map implementation built for this, like the BiMap from google collections.使用为此构建的地图实现,例如来自谷歌集合的BiMap Note that the google collections BiMap requires uniqueless of values, as well as keys, but it provides high performance in both directions performance请注意,谷歌集合 BiMap 需要唯一的值和键,但它提供了双向性能的高性能
  • Manually maintain two maps - one for key -> value, and another map for value -> key手动维护两个映射 - 一个用于键 -> 值,另一个映射用于值 -> 键
  • Iterate through the entrySet() and to find the keys which match the value.遍历entrySet()并找到与值匹配的键。 This is the slowest method, since it requires iterating through the entire collection, while the other two methods don't require that.这是最慢的方法,因为它需要遍历整个集合,而其他两种方法不需要。

Using Java 8:使用 Java 8:

ftw.forEach((key, value) -> {
    if (value.equals("foo")) {
        System.out.print(key);
    }
});

Decorate map with your own implementation使用您自己的实现装饰地图

class MyMap<K,V> extends HashMap<K, V>{

    Map<V,K> reverseMap = new HashMap<V,K>();

    @Override
    public V put(K key, V value) {
        // TODO Auto-generated method stub
        reverseMap.put(value, key);
        return super.put(key, value);
    }

    public K getKey(V value){
        return reverseMap.get(value);
    }
}

There is no unambiguous answer, because multiple keys can map to the same value.没有明确的答案,因为多个键可以映射到相同的值。 If you are enforcing unique-ness with your own code, the best solution is to create a class that uses two Hashmaps to track the mappings in both directions.如果您使用自己的代码强制唯一性,最好的解决方案是创建一个类,该类使用两个 Hashmap 来跟踪两个方向的映射。

要查找映射到该值的所有键,请使用map.entrySet()遍历哈希图中的所有对。

I think this is best solution, original address: Java2s我认为这是最好的解决方案,原文地址: Java2s

    import java.util.HashMap;
    import java.util.Map;

        public class Main {

          public static void main(String[] argv) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("1","one");
            map.put("2","two");
            map.put("3","three");
            map.put("4","four");

            System.out.println(getKeyFromValue(map,"three"));
          }


// hm is the map you are trying to get value from it
          public static Object getKeyFromValue(Map hm, Object value) {
            for (Object o : hm.keySet()) {
              if (hm.get(o).equals(value)) {
                return o;
              }
            }
            return null;
          }
        }

An easy usage: if you put all data in hasMap and you have item = "Automobile", so you are looking its key in hashMap.一个简单的用法:如果你把所有数据都放在 hasMap 并且你有 item = "Automobile",那么你在 hashMap 中查找它的键。 that is good solution.这是一个很好的解决方案。

getKeyFromValue(hashMap, item);
System.out.println("getKeyFromValue(hashMap, item): "+getKeyFromValue(hashMap, item));

If you build the map in your own code, try putting the key and value in the map together:如果您在自己的代码中构建地图,请尝试将地图中的键和值放在一起:

public class KeyValue {
    public Object key;
    public Object value;
    public KeyValue(Object key, Object value) { ... }
}

map.put(key, new KeyValue(key, value));

Then when you have a value, you also have the key.然后,当您拥有价值时,您也拥有了钥匙。

I'm afraid you'll just have to iterate your map.恐怕你只需要迭代你的地图。 Shortest I could come up with:我能想到的最短时间:

Iterator<Map.Entry<String,String>> iter = map.entrySet().iterator();
while (iter.hasNext()) {
    Map.Entry<String,String> entry = iter.next();
    if (entry.getValue().equals(value_you_look_for)) {
        String key_you_look_for = entry.getKey();
    }
}
for(int key: hm.keySet()) {
    if(hm.get(key).equals(value)) {
        System.out.println(key); 
    }
}

听起来最好的方法是使用map.entrySet()迭代条目,因为map.containsValue()可能无论如何都map.containsValue()

For Android development targeting API < 19, Vitalii Fedorenko one-to-one relationship solution doesn't work because Objects.equals isn't implemented.对于面向 API < 19 的 Android 开发,Vitalii Fedorenko 一对一关系解决方案不起作用,因为Objects.equals未实现。 Here's a simple alternative:这是一个简单的替代方案:

public <K, V> K getKeyByValue(Map<K, V> map, V value) {
    for (Map.Entry<K, V> entry : map.entrySet()) {
            if (value.equals(entry.getValue())) {
            return entry.getKey();
        }
    }
    return null;
}

You can use the below:您可以使用以下内容:

public class HashmapKeyExist {
    public static void main(String[] args) {
        HashMap<String, String> hmap = new HashMap<String, String>();
        hmap.put("1", "Bala");
        hmap.put("2", "Test");

        Boolean cantain = hmap.containsValue("Bala");
        if(hmap.containsKey("2") && hmap.containsValue("Test"))
        {
            System.out.println("Yes");
        }
        if(cantain == true)
        {
            System.out.println("Yes"); 
        }

        Set setkeys = hmap.keySet();
        Iterator it = setkeys.iterator();

        while(it.hasNext())
        {
            String key = (String) it.next();
            if (hmap.get(key).equals("Bala"))
            {
                System.out.println(key);
            }
        }
    }
}

I think keySet() may be well to find the keys mapping to the value, and have a better coding style than entrySet() .我认为keySet()可以很好地找到映射到值的键,并且比entrySet()具有更好的编码风格。

Ex:前任:

Suppose you have a HashMap map , ArrayList res , a value you want to find all the key mapping to , then store keys to the res .假设你有一个HashMap的地图,ArrayList的资源,要找到所有的键映射到一个,然后将密钥存储到水库

You can write code below:您可以在下面编写代码:

    for (int key : map.keySet()) {
        if (map.get(key) == value) {
            res.add(key);
        }
    }

rather than use entrySet() below:而不是在下面使用 entrySet() :

    for (Map.Entry s : map.entrySet()) {
        if ((int)s.getValue() == value) {
            res.add((int)s.getKey());
        }
    }

Hope it helps :)希望能帮助到你 :)

Yes, you have to loop through the hashmap, unless you implement something along the lines of what these various answers suggest.是的,您必须遍历哈希图,除非您按照这些不同答案所建议的方式实现某些内容。 Rather than fiddling with the entrySet, I'd just get the keySet(), iterate over that set, and keep the (first) key that gets you your matching value.而不是摆弄 entrySet,我只是获取 keySet(),迭代该集合,并保留获得匹配值的(第一个)键。 If you need all the keys that match that value, obviously you have to do the whole thing.如果您需要与该值匹配的所有键,显然您必须做整件事。

As Jonas suggests, this might already be what the containsValue method is doing, so you might just skip that test all-together, and just do the iteration every time (or maybe the compiler will already eliminate the redundancy, who knows).正如 Jonas 所建议的,这可能已经是 containsValue 方法正在执行的操作,因此您可以一起跳过该测试,并且每次都进行迭代(或者编译器可能已经消除了冗余,谁知道呢)。

Also, relative to the other answers, if your reverse map looks like另外,相对于其他答案,如果您的反向地图看起来像

Map<Value, Set<Key>>

you can deal with non-unique key->value mappings, if you need that capability (untangling them aside).如果您需要该功能(将它们解开),您可以处理非唯一键-> 值映射。 That would incorporate fine into any of the solutions people suggest here using two maps.这将很好地融入人们在这里使用两张地图建议的任何解决方案中。

public static class SmartHashMap <T1 extends Object, T2 extends Object> {
    public HashMap<T1, T2> keyValue;
    public HashMap<T2, T1> valueKey;

    public SmartHashMap(){
        this.keyValue = new HashMap<T1, T2>();
        this.valueKey = new HashMap<T2, T1>();
    }

    public void add(T1 key, T2 value){
        this.keyValue.put(key, value);
        this.valueKey.put(value, key);
    }

    public T2 getValue(T1 key){
        return this.keyValue.get(key);
    }

    public T1 getKey(T2 value){
        return this.valueKey.get(value);
    }

}

In java8在java8中

map.entrySet().stream().filter(entry -> entry.getValue().equals(value))
    .forEach(entry -> System.out.println(entry.getKey()));

You can get the key using values using following code..您可以使用以下代码使用值获取密钥..

ArrayList valuesList = new ArrayList();
Set keySet = initalMap.keySet();
ArrayList keyList = new ArrayList(keySet);

for(int i = 0 ; i < keyList.size() ; i++ ) {
    valuesList.add(initalMap.get(keyList.get(i)));
}

Collections.sort(valuesList);
Map finalMap = new TreeMap();
for(int i = 0 ; i < valuesList.size() ; i++ ) {
    String value = (String) valuesList.get(i);

    for( int j = 0 ; j < keyList.size() ; j++ ) {
        if(initalMap.get(keyList.get(j)).equals(value)) {
            finalMap.put(keyList.get(j),value);
        }   
    }
}
System.out.println("fianl map ---------------------->  " + finalMap);
public static String getKey(Map<String, Integer> mapref, String value) {
    String key = "";
    for (Map.Entry<String, Integer> map : mapref.entrySet()) {
        if (map.getValue().toString().equals(value)) {
            key = map.getKey();
        }
    }
    return key;
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class M{
public static void main(String[] args) {

        HashMap<String, List<String>> resultHashMap = new HashMap<String, List<String>>();

        Set<String> newKeyList = resultHashMap.keySet();


        for (Iterator<String> iterator = originalHashMap.keySet().iterator(); iterator.hasNext();) {
            String hashKey = (String) iterator.next();

            if (!newKeyList.contains(originalHashMap.get(hashKey))) {
                List<String> loArrayList = new ArrayList<String>();
                loArrayList.add(hashKey);
                resultHashMap.put(originalHashMap.get(hashKey), loArrayList);
            } else {
                List<String> loArrayList = resultHashMap.get(originalHashMap
                        .get(hashKey));
                loArrayList.add(hashKey);
                resultHashMap.put(originalHashMap.get(hashKey), loArrayList);
            }
        }

        System.out.println("Original HashMap : " + originalHashMap);
        System.out.println("Result HashMap : " + resultHashMap);
    }
}
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class ValueKeysMap<K, V> extends HashMap <K,V>{
    HashMap<V, Set<K>> ValueKeysMap = new HashMap<V, Set<K>>();

    @Override
    public boolean containsValue(Object value) {
        return ValueKeysMap.containsKey(value);
    }

    @Override
    public V put(K key, V value) {
        if (containsValue(value)) {
            Set<K> keys = ValueKeysMap.get(value);
            keys.add(key);
        } else {
            Set<K> keys = new HashSet<K>();
            keys.add(key);
            ValueKeysMap.put(value, keys);
        }
        return super.put(key, value);
    }

    @Override
    public V remove(Object key) {
        V value = super.remove(key);
        Set<K> keys = ValueKeysMap.get(value);
        keys.remove(key);
        if(keys.size() == 0) {
           ValueKeysMap.remove(value);
        }
        return value;
    }

    public Set<K> getKeys4ThisValue(V value){
        Set<K> keys = ValueKeysMap.get(value);
        return keys;
    }

    public boolean valueContainsThisKey(K key, V value){
        if (containsValue(value)) {
            Set<K> keys = ValueKeysMap.get(value);
            return keys.contains(key);
        }
        return false;
    }

    /*
     * Take care of argument constructor and other api's like putAll
     */
}
/**
 * This method gets the Key for the given Value
 * @param paramName
 * @return
 */
private String getKeyForValueFromMap(String paramName) {
    String keyForValue = null;
    if(paramName!=null)) {
        Set<Entry<String,String>> entrySet = myMap().entrySet();
        if(entrySet!=null && entrySet.size>0) {
            for(Entry<String,String> entry : entrySet) {
                if(entry!=null && paramName.equalsIgnoreCase(entry.getValue())) {
                    keyForValue = entry.getKey();
                }
            }
        }
    }
    return keyForValue;
}

My 2 cents.我的 2 美分。 You can get the keys in an array and then loop through the array.您可以获取数组中的键,然后遍历数组。 This will affect performance of this code block if the map is pretty big , where in you are getting the keys in an array first which might consume some time and then you are looping.如果地图非常大,这将影响此代码块的性能,其中您首先在数组中获取键,这可能会消耗一些时间,然后您正在循环。 Otherwise for smaller maps it should be ok.否则对于较小的地图应该没问题。

String[] keys =  yourMap.keySet().toArray(new String[0]);

for(int i = 0 ; i < keys.length ; i++){
    //This is your key    
    String key = keys[i];

    //This is your value
    yourMap.get(key)            
}

While this does not directly answer the question, it is related.虽然这不能直接回答问题,但它是相关的。

This way you don't need to keep creating/iterating.这样你就不需要继续创建/迭代。 Just create a reverse map once and get what you need.只需创建一次反向地图即可获得所需内容。

/**
 * Both key and value types must define equals() and hashCode() for this to work.
 * This takes into account that all keys are unique but all values may not be.
 *
 * @param map
 * @param <K>
 * @param <V>
 * @return
 */
public static <K, V> Map<V, List<K>> reverseMap(Map<K,V> map) {
    if(map == null) return null;

    Map<V, List<K>> reverseMap = new ArrayMap<>();

    for(Map.Entry<K,V> entry : map.entrySet()) {
        appendValueToMapList(reverseMap, entry.getValue(), entry.getKey());
    }

    return reverseMap;
}


/**
 * Takes into account that the list may already have values.
 * 
 * @param map
 * @param key
 * @param value
 * @param <K>
 * @param <V>
 * @return
 */
public static <K, V> Map<K, List<V>> appendValueToMapList(Map<K, List<V>> map, K key, V value) {
    if(map == null || key == null || value == null) return map;

    List<V> list = map.get(key);

    if(list == null) {
        List<V> newList = new ArrayList<>();
        newList.add(value);
        map.put(key, newList);
    }
    else {
        list.add(value);
    }

    return map;
}

Simplest utility method to fetch a key of a given value from a Map:从 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);    
}

detailed explaination:详细说明:

  1. Method fetchValue accepts the map, which has String as key and Integer as value.方法 fetchValue 接受映射,它以 String 为键,以 Integer 为值。

  2. Then we use entryset().stream() to convert result into a stream.然后我们使用 entryset().stream() 将结果转换为流。

  3. Next we use filter (intermediate operation) which gives us a value that is equal to the second argument.接下来我们使用过滤器(中间操作),它给我们一个等于第二个参数的值。

  4. Finally, we use forEach(final operation) to print our end result.最后,我们使用 forEach(final operation) 来打印我们的最终结果。

Use a thin wrapper: HMap使用薄包装: HMap

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class HMap<K, V> {

   private final Map<K, Map<K, V>> map;

   public HMap() {
      map = new HashMap<K, Map<K, V>>();
   }

   public HMap(final int initialCapacity) {
      map = new HashMap<K, Map<K, V>>(initialCapacity);
   }

   public boolean containsKey(final Object key) {
      return map.containsKey(key);
   }

   public V get(final Object key) {
      final Map<K, V> entry = map.get(key);
      if (entry != null)
         return entry.values().iterator().next();
      return null;
   }

   public K getKey(final Object key) {
      final Map<K, V> entry = map.get(key);
      if (entry != null)
         return entry.keySet().iterator().next();
      return null;
   }

   public V put(final K key, final V value) {
      final Map<K, V> entry = map
            .put(key, Collections.singletonMap(key, value));
      if (entry != null)
         return entry.values().iterator().next();
      return null;
   }
}

It's important to note that since this question, Apache Collections supports Generic BidiMaps .重要的是要注意,由于这个问题,Apache Collections 支持Generic BidiMaps So a few of the top voted answers are no longer accurate on that point.因此,在这一点上,一些投票最高的答案不再准确。

For a Serialized BidiMap that also supports duplicate values ( 1-to-many scenario ) also consider MapDB.org .对于还支持重复值(一对多场景)的序列化 BidiMap,还可以考虑MapDB.org

If I have the value "foo" , and a HashMap<String> ftw for which ftw.containsValue("foo") returns true , how can I get the corresponding key?如果我的值是"foo" ,并且ftw.containsValue("foo")返回trueHashMap<String> ftw ,如何获得相应的键? Do I have to loop through the hashmap?我是否必须遍历哈希图? What is the best way to do that?最好的方法是什么?

try this:试试这个:

static String getKeyFromValue(LinkedHashMap<String, String> map,String value) {
    for (int x=0;x<map.size();x++){
        if( String.valueOf( (new ArrayList<String>(map.values())).get(x) ).equals(value))
            return String.valueOf((new ArrayList<String>(map.keySet())).get(x));
    }
    return null;
}

As far as I know keys and values of a HashMap are not mixed when you represent them as arrays:据我所知,当您将 HashMap 的键和值表示为数组时,它们不会混合:

hashmap.values().toArray()

and

hashmap.keySet().toArray()

So the following code (since java 8) should work as expected:因此,以下代码(自 java 8 起)应按预期工作:

public Object getKeyByFirstValue(Object value) {
    int keyNumber =  Arrays.asList(hashmap.values().toArray()).indexOf(value);
    return hashmap.keySet().toArray()[keyNumber];
}

However, ( WARNING! ) it works 2-3 times slower than iteration.但是,(警告! )它的工作速度比迭代慢 2-3 倍。

Let the value be maxValue .让该值为maxValue

Set keySet = map.keySet();

keySet.stream().filter(x->map.get(x)==maxValue).forEach(x-> System.out.println(x));

lambda w/o use of external libraries不使用外部库的 lambda
can deal with multiple values for one key (in difference to the BidiMap)可以为一个key处理多个值(与BidiMap不同)

public static List<String> getKeysByValue(Map<String, String> map, String value) {
  List<String> list = map.keySet().stream()
      .collect(groupingBy(k -> map.get(k))).get(value);
  return (list == null ? Collections.emptyList() : list);
}

gets a List containing the key(s) mapping value得到一个List包含key(s)的映射value
for an 1:1 mapping the returned list is empty or contains 1 value对于 1:1 映射,返回的列表为empty或包含 1 个value

let see my example看看我的例子

Map<String, String> mapPeopleAndCountry = new HashMap<>();
mapPeopleAndCountry.put("Matis", "Lithuania");
mapPeopleAndCountry.put("Carlos", "Honduras");
mapPeopleAndCountry.put("Teboho", "Lesotho");
mapPeopleAndCountry.put("Marielos", "Honduras");


List<String> peopleInHonduras = mapPeopleAndCountry.keySet()
    .stream()
    .filter(r -> mapPeopleAndCountry.get(r)
                .equals("Honduras"))
    .stream(Collectors.toList());

// will return ["Carlos", "Marielos"]

Note: untested, may contains typo注意:未经测试,可能包含错字

Found too many answers.找到了太多答案。 Some were really great.有些真的很棒。 But I was particularly looking for a way, so that I can get the value using loops.但我特别在寻找一种方法,以便我可以使用循环获取值。

So here is finally what I did: For a HashMap 1-to-1 relation:所以这终于是我所做的:对于 HashMap 1 对 1 关系:

    Map<String, String> map = new HashMap<String, String>();
    
    map.put("abc", "123");
    map.put("xyz", "456");
    
    for(Entry<String, String> entry : map.entrySet()) {
        if(entry.getValue().equalsIgnoreCase("456")) {
            System.out.println(entry.getKey());
        }
    }

Output: "xyz"输出:“xyz”

For a HashMap 1-to-many relation:对于 HashMap 一对多关系:

    Map<String, ArrayList<String>> service = new HashMap<String, ArrayList<String>>();
    
    service.put("abc", new ArrayList<String>());
    service.get("abc").add("a");
    service.get("abc").add("b");
    service.get("abc").add("c");
    
    service.put("xyz", new ArrayList<String>());
    service.get("xyz").add("x");
    service.get("xyz").add("y");
    service.get("xyz").add("z");
    
    for(Entry<String, ArrayList<String>> entry : service.entrySet()) {
        ArrayList<String> values = entry.getValue();
        for(String value : values) {
            if(value.equalsIgnoreCase("x")) {
                System.out.println(entry.getKey());
            }
        }
        
    }

Output: xyz输出:xyz

-Thanks -谢谢

你也可以做这个工作:第一:放置映射(键,值)第二:更新你需要删除表达式的键第三:并用 oldValue 放置一个新键

Iterator<Map.Entry<String,String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
    Map.Entry<String,String> entry = iterator.next();
    if (entry.getValue().equals(value_you_look_for)) {
        String key_you_look_for = entry.getKey();
}
} 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM