HashMap and int as key

前端 未结 11 1995
说谎
说谎 2020-11-28 23:32

I am trying to build a HashMap which will have integer as keys and objects as values.

My syntax is:

HashMap myMap = new HashMap&         


        
相关标签:
11条回答
  • 2020-11-29 00:04

    For everybody who codes Java for Android devices and ends up here: use SparseArray for better performance;

    private final SparseArray<myObject> myMap = new SparseArray<myObject>();
    

    with this you can use int instead of Integer like;

    int newPos = 3;
    
    myMap.put(newPos, newObject);
    myMap.get(newPos);
    
    0 讨论(0)
  • 2020-11-29 00:05

    Use Integer instead.

    HashMap<Integer, MyObject> myMap = new HashMap<Integer, MyObject>();
    

    Java will automatically autobox your int primitive values to Integer objects.

    Read more about autoboxing from Oracle Java documentations.

    0 讨论(0)
  • 2020-11-29 00:12

    HashMap does not allow primitive data types as arguments. It can only accept objects so

    HashMap<int, myObject> myMap = new HashMap<int, myObject>();
    

    will not work.

    You have to change the declaration to

    HashMap<Integer, myObject> myMap = new HashMap<Integer, myObject>();
    

    so even when you do the following

    myMap.put(2,myObject);
    

    The primitive data type is autoboxed to an Integer object.

    8 (int) === boxing ===> 8 (Integer)
    

    You can read more on autoboxing here http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html

    0 讨论(0)
  • 2020-11-29 00:13

    You can't use a primitive because HashMap use object internally for the key. So you can only use an object that inherits from Object (that is any object).

    That is the function put() in HashMap and as you can see it uses Object for K:

    public V put(K key, V value) {
        if (key == null)
            return putForNullKey(value);
        int hash = hash(key);
        int i = indexFor(hash, table.length);
        for (Entry<K,V> e = table[i]; e != null; e = e.next) {
            Object k;
            if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
                V oldValue = e.value;
                e.value = value;
                e.recordAccess(this);
                return oldValue;
            }
        }
    
        modCount++;
        addEntry(hash, key, value, i);
        return null;
    }
    

    The expression "k = e.key" should make it clear.

    I suggest to use a wrapper like Integer and autoboxing.

    0 讨论(0)
  • 2020-11-29 00:16

    The main reason with HashMap not allowing primitive as keys is that HashMap is designed in such a way that for comparing the keys, it makes use of equals() method, and a method can be called only on an object not on a primitive.

    Thus when int is autoboxed to Integer, Hashmap can call equals() method on Integer object.

    That is why, you should use Integer instead of int. I mean hashmap throws an error while putting int as a key (Don't know the meaning of the error that is thrown)

    And if you think that, you can make Map performance faster by making a primitive as a key, there is a library called FastUtil which contains a Map implementation with int type as a key.

    Because of this, it is much faster than Hashmap

    0 讨论(0)
提交回复
热议问题