Map根据值获取key

发布时间:2022-12-16 16:30

Map根据值获取key,找到一篇大佬的文章,本文是CV过来的,
原文:https://www.techiedelight.com...

1.entrySet() 方法

import java.util.HashMap;
import java.util.Map;
 
class Main
{
    public static  K getKey(Map map, V value)
    {
        for (Map.Entry entry: map.entrySet())
        {
            if (value.equals(entry.getValue())) {
                return entry.getKey();
            }
        }
        return null;
    }

    // Java8
    public static  K getKey(Map map, V value)
    {
        return map.entrySet().stream()
                .filter(entry -> value.equals(entry.getValue()))
                .findFirst().map(Map.Entry::getKey)
                .orElse(null);
    }
 
    public static void main(String[] args)
    {
        Map hashMap = new HashMap();
        hashMap.put("A", 1);
        hashMap.put("B", 2);
        hashMap.put("C", 3);
 
        System.out.println(getKey(hashMap, 2));        // prints `B`
    }
}

2.keySet() 方法

import java.util.HashMap;
import java.util.Map;
 
class Main
{
    public static  K getKey(Map map, V value)
    {
        for (K key: map.keySet())
        {
            if (value.equals(map.get(key))) {
                return key;
            }
        }
        return null;
    }

    // Java8
    public static  K getKey(Map map, V value)
    {
        return map.keySet()
                    .stream()
                    .filter(key -> value.equals(map.get(key)))
                    .findFirst().get();
    }
 
    public static void main(String[] args)
    {
        Map hashMap = new HashMap();
        hashMap.put("A", 1);
        hashMap.put("B", 2);
        hashMap.put("C", 3);
 
        System.out.println(getKey(hashMap, 2));        // prints `B`
    }
}

3.反转Map

import java.util.HashMap;
import java.util.Map;
 
class MyHashMap extends HashMap
{
    Map reverseMap = new HashMap<>();
 
    @Override
    public V put(K key, V value)
    {
        reverseMap.put(value, key);
        return super.put(key, value);
    }
 
    public K getKey(V value) {
        return reverseMap.get(value);
    }
 
}
 
class Main
{
    public static void main(String[] args)
    {
        MyHashMap hashMap = new MyHashMap();
        hashMap.put("A", 1);
        hashMap.put("B", 2);
        hashMap.put("C", 3);
 
        System.out.println(hashMap.getKey(2));        // prints `B`
    }
}

4.Guava’s BiMap类

import com.google.common.collect.BiMap;
import com.google.common.collect.ImmutableBiMap;
 
class Main
{
    public static  K getKey(BiMap map, V value) {
        return map.inverse().get(value);
    }
 
    public static void main(String[] args)
    {
        BiMap bimap = ImmutableBiMap.of("A", 1, "B", 2, "C", 3);
 
        System.out.println(getKey(bimap, 2));        // prints `B`
    }
}

5.Apache Commons Collections

import org.apache.commons.collections4.BidiMap;
import org.apache.commons.collections4.bidimap.DualHashBidiMap;
 
class Main
{
    public static  K getKey(BidiMap map, V value) {
        return map.inverseBidiMap().get(value);
    }
 
    public static void main(String[] args)
    {
        BidiMap bimap = new DualHashBidiMap<>();
        bimap.put("A", 1);
        bimap.put("B", 2);
        bimap.put("C", 3);
 
        System.out.println(getKey(bimap, 2));        // prints `B`
    }
}

原文:https://www.techiedelight.com...

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号