简体   繁体   English

更快地从Map中找出给定值的关键字?

[英]Faster way to find out the key for given value from Map?

我想从HashMap中找出给定值的关键,目前我必须通过所有键并在地图中检查它的值,有更快的方法吗?

An alternate data structure for doing this would be a BiMap from the google collections API. 用于执行此操作的备用数据结构将是来自google collections API的BiMap

The API doc is here . API文档在这里

No, there is not a faster way (without introducing other data structures). 不,没有更快的方法(没有引入其他数据结构)。 If you need to do this often, reconsider your design. 如果您需要经常这样做,请重新考虑您的设计。 Maybe you want another HashMap whose keys are the values of the other HashMap ? 也许你想另一个HashMap的键是其他的值HashMap

This would be the simplest way to do this 这将是最简单的方法

private Object getKey(LinkedHashMap lm, int val){

    Object[] j = lm.keySet().toArray();
    return j[val];
}

Here are two related posts on stackoverflow: 这是stackoverflow上的两个相关帖子:

  1. Does Java have a HashMap with reverse lookup? Java是否有反向查找的HashMap?
  2. Bi-directional Map in Java? Java中的双向映射?

Some solution hadn't been mentioned yet BidiMap : BidiMap还没有提到一些解决方案:

The source code is from here : 源代码来自这里

package com.discursive.jccook.collections.bidi;
import org.apache.commons.collections.BidiMap;
import org.apache.commons.collections.bidimap.DualHashBidiMap;
public class BidiMapExample {
    private BidiMap countryCodes = new DualHashBidiMap( );
    public static void main(String[] args) {
        BidiMapExample example = new BidiMapExample( );
        example.start( );
    }

    private void start( ) {
        populateCountryCodes( );

        String countryName = (String) countryCodes.get( "tr" );
        System.out.println( "Country Name for code 'tr': " + countryName );
        String countryCode = 
            (String) countryCodes.inverseBidiMap( ).get("Uruguay");
        System.out.println( "Country Code for name 'Uruguay': " + countryCode );

        countryCode = (String) countryCodes.getKey("Ukraine");
        System.out.println( "Country Code for name 'Ukraine': " + countryCode );
    }

    private void populateCountryCodes( ) {
        countryCodes.put("to","Tonga");
        countryCodes.put("tr","Turkey");
        countryCodes.put("tv","Tuvalu");
        countryCodes.put("tz","Tanzania");
        countryCodes.put("ua","Ukraine");
        countryCodes.put("ug","Uganda");
        countryCodes.put("uk","United Kingdom");
        countryCodes.put("um","USA Minor Outlying Islands");
        countryCodes.put("us","United States");
        countryCodes.put("uy","Uruguay");
    }
}

If you look at HashMap.get(key) method you will see a use of 如果你看一下HashMap.get(key)方法,你会看到使用

Entry entry = getEntry (key); Entry entry = getEntry (key);

Why is getEntry (key) method private?? 为什么getEntry (key)方法是私有的? It returns key and value for desired search. 它返回所需搜索的键和值。 I will use this method by brute force, but it is ugly solution... 我将通过蛮力使用这种方法,但它是丑陋的解决方案......

Implementation of method in HashMap follows 下面是HashMap中方法的实现

/**
 * Returns the entry associated with the specified key in the
 * HashMap.  Returns null if the HashMap contains no mapping
 * for the key.
 */
final Entry<K,V> getEntry(Object key) {
    int hash = (key == null) ? 0 : hash(key);
    for (Entry<K,V> e = table[indexFor(hash, table.length)];
         e != null;
         e = e.next) {
        Object k;
        if (e.hash == hash &&
            ((k = e.key) == key || (key != null && key.equals(k))))
            return e;
    }
    return null;
}

使用更好的数据结构,如TreeMap,因为搜索效率会更高。

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

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