java中的嵌套地图或组合键 [英] Nested Maps or combined keys in java

查看:119
本文介绍了java中的嵌套地图或组合键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个Map来在Java中创建一个缓存,以获得具有两个String键的相同值。我的问题最好是嵌套地图(每个键一个)或者使用两个字符串创建某种类型的自定义键?



访问缓存中的数据将始终用这两个键访问,我不需要用任何两个键来分组。



然后如果更好地将字符串键合并在一起,它会更好吗? / p>


  • 使用自定义 getHash 方法的自定义类。但问题是什么哈希函数实现?

  • 简单地连接两个字符串。例如:

    cache.put(key1 + key2,value)



  • hashCode()。



    连接键通常不是一个好主意,最终可能会发生冲突,例如键 1 22 和键 12 2 。他们会映射到相同的值 122



    如果您始终使用两个键, Map 总会更有效一些,您可以随时在地图上定义自己的适配器,它将包含两个参数:

      public class MyCache {

    private Map< MyKey,Object> cache = new HashMap< MyKey,Object>();

    public Object getObject(Object key1,Object key2){
    return cache.get(new MyKey(key1,key2));
    }
    public void putObject(Object key1,Object key2,Object value){
    cache.put(new MyKey(key1,key2),value);


    请记住定义 equals ) hashCode()在您的自定义关键类中添加(如果需要,添加检查是否为空)。

      public int hashCode(){
    int result = 17;
    result = 37 * result + keyA.hashCode();
    result = 37 * result + keyB.hashCode();
    返回结果;
    }

    public boolean equals(Object another){
    return another.keyA.equals(keyA)&& another.keyB.equals(KEYB);
    }


    I need a Map to make a cache in Java for same values that I have two String keys. My question it's better to make nested Maps (one for each key) or make some type of custom key maked with the two Strings?

    Access to data on cache will all time accessed with the two keys and I don't need group it by any of that two keys.

    Then if is better combine string key in only one what it's better?

    • Custom class with custom getHash method. But then the problem is what hash function implement?
    • Simply concatenate two strings together. For example:

      cache.put(key1+key2, value)

    解决方案

    You can either make nested maps or use a custom class defining hashCode().

    It's usually not a good idea to concatenate the keys, you can end up with collisions, as in the case with keys 1 and 22 and keys 12 and 2. They'd map to the same value 122.

    If you'll always use both keys, using a single Map will always be a little more efficient, and you can always define your own adapter to the map that will take two arguments:

    public class MyCache { 
    
        private Map<MyKey, Object> cache = new HashMap<MyKey, Object>();
    
        public Object getObject(Object key1, Object key2){
            return cache.get(new MyKey(key1, key2));
        }
        public void putObject(Object key1, Object key2, Object value){
            cache.put(new MyKey(key1, key2), value);
        }
    }
    

    Remember to define equals() and hashCode() in your custom key class (add checks for nullity if needed).

    public int hashCode() {
        int result = 17;
        result = 37 * result + keyA.hashCode();
        result = 37 * result + keyB.hashCode();
        return result;
    }
    
    public boolean equals(Object another) { 
        return another.keyA.equals(keyA) && another.keyB.equals(keyB);
    } 
    

    这篇关于java中的嵌套地图或组合键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆