Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is faster, String or Integer as hashkey in Java?

Tags:

java

hashtable

I am working on a problem and I have problem with execution times becoming too large and now Im looking for possible optimizations.

The question: Is there any (considerable) difference in performance between using String or Integer as haskey?

The problem is I have a graph with nodes stored in a hashtable with String as key. For example the keys are as follows - "0011" or "1011" etc. Now I could convert these to integers aswell if this would mean a improvement in execution time.

like image 891
Euklides Avatar asked Mar 26 '13 08:03

Euklides


People also ask

Why do we prefer string as key in HashMap?

String is as a key of the HashMapWhen you pass the key to retrieve its value, the hash code is calculated again, and the value in the position represented by the hash code is fetched (if both hash codes are equal).

Why Hashmaps are fast?

Hashmaps use the hashcode of the key to access directly the bucket where the entry is stored. This is an O(1) access. If more than one element is in that bucket because of the same or similar hashcode, then you have a few more checks, but it's still way faster than iterating through a list and searching for an element.

Can a HashMap key be an integer?

Here, keys are used as identifiers which are used to associate each value on a map. It can store different types: Integer keys and String values or same types: Integer keys and Integer values. HashMap is similar to HashTable, but it is unsynchronized.

What is Hashkey in Java?

It is similar to HashMap, but is synchronized. Hashtable stores key/value pair in hash table. In Hashtable we specify an object that is used as a key, and the value we want to associate to that key. The key is then hashed, and the resulting hash code is used as the index at which the value is stored within the table.


3 Answers

Integer will perform better than String. Following is code for the hashcode computation for both.

Integer hash code implementation

/**
     * Returns a hash code for this <code>Integer</code>.
     *
     * @return  a hash code value for this object, equal to the 
     *          primitive <code>int</code> value represented by this 
     *          <code>Integer</code> object. 
     */
    public int hashCode() {
    return value;
    }

String hash code implementation

 /**
     * Returns a hash code for this string. The hash code for a
     * <code>String</code> object is computed as
     * <blockquote><pre>
     * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
     * </pre></blockquote>
     * using <code>int</code> arithmetic, where <code>s[i]</code> is the
     * <i>i</i>th character of the string, <code>n</code> is the length of
     * the string, and <code>^</code> indicates exponentiation.
     * (The hash value of the empty string is zero.)
     *
     * @return  a hash code value for this object.
     */
    public int hashCode() {
    int h = hash;
    if (h == 0) {
        int off = offset;
        char val[] = value;
        int len = count;

            for (int i = 0; i < len; i++) {
                h = 31*h + val[off++];
            }
            hash = h;
        }
        return h;
    }
like image 183
Jabir Avatar answered Nov 02 '22 00:11

Jabir


If you have performance problem, it's quite unlikely that the issue is due to the HashMap/HashTable. While hashing string is slightly more expensive than hashing integers, it's rather small difference, and hashCode is cached so it's not recalculated if you use the same string object, you are unlikely to get any significant performance benefit from converting it first to integer.

It's probably more fruitful to look somewhere else for the source of your performance issue. Have you tried profiling your code yet?

like image 31
Lie Ryan Avatar answered Nov 01 '22 23:11

Lie Ryan


There is a difference in speed. HashMaps will use hashCode to calculate the bucket based on that code, and the implementation of Integer is much simpler than that of String.

Having said that, if you are having problems with execution times, you need to do some proper measurements and profiling yourself. That's the only way to find out what the problem is with the execution time, and using Integers instead of Strings will usually only have a minimal effect on performance, meaning that your performance problem might be elsewhere.

For example, look at this post if you want to do some proper micro benchmarks. There are many other resources available for profiling etc..

like image 42
Erik Pragt Avatar answered Nov 01 '22 23:11

Erik Pragt