HashMap with byte array key and String value - containsKey() function doesn't work

丶灬走出姿态 提交于 2019-12-09 03:36:43

问题


I'm using a HashMap: byte[] key and String value. But I realize that even I put the same object (same byte array and same string value) by using

myList.put(TheSameByteArray, TheSameStringValue)

into HashMap, the table still inserts a new object with different HashMapEntry. Then function containsKey() cannot work.

Can someone explains this for me? How can I fix this? Thanks. (Android Java)

@Override public boolean containsKey(Object key) {
    if (key == null) {
        return entryForNullKey != null;
    }

    int hash = Collections.secondaryHash(key);
    HashMapEntry<K, V>[] tab = table;
    for (HashMapEntry<K, V> e = tab[hash & (tab.length - 1)];
            e != null; e = e.next) {
        K eKey = e.key;
        if (eKey == key || (e.hash == hash && key.equals(eKey))) {
            return true;
        }
    }
    return false;
}

回答1:


A byte[] (or any array) can't work properly as a key in a HashMap, since arrays don't override equals, so two arrays will be considered equal only if they refer to the same object.

You'll have to wrap your byte[] in some custom class that overrides hashCode and equals, and use that custom class as the key to your HashMap.




回答2:


Adding to Eran's clear answer,Since byte[] or any array doesnt override hashcode and equals(it uses the default methods of Object class ),you can always wrap around a String Object which takes byte[] as constructor argument.Not only does String form good keys in Map,they are immutable too(the operations in a Hash based map are faster)

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#String(byte[])




回答3:


NOTE: This is an extremely hack-y way of making an array or a string, a key in a HashMap, without overriding either the equals() or the hashCode() methods. I'll include the answer in a generic way, so readers can get the idea and implement as per their requirements.

Say, I have two numbers, n and r. I want a key-value pair with [n,r] as the key, and (n+r) as the value.

Map<List<Integer>, Integer> map = new HashMap<List<Integer>, Integer>();

List<Integer> key = Arrays.asList(n, r);
if( map.containsKey(key) )
    return map.get(key);

What if the map did not contain the key?

map.put(Collections.unmodifiableList(Arrays.asList(n, r)), (n+r));

The unmodifiable part (without going into any further depth), ensures that the key cannot change the hash code.

Now, map.containsKey(key) will be true.

Note: This is not a good way to do it. It is just a workaround.



来源:https://stackoverflow.com/questions/31938180/hashmap-with-byte-array-key-and-string-value-containskey-function-doesnt-wo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!