Is it better to use Objects or integers as HashMap keys?

How does hashing of objects work in java's HashMap? I was thinking if it is more efficient to use integers as keys compared to Strings or if it does not matter.

If I have:

String str = "hello";
Object helloObject = new Object();

What is better in case of String? Use integer key:

HashMap<Integer, Object> hashes = new HashMap<Integer, Object>();
hashes.put(str.hashCode(), helloObject);

or use String key?

HashMap<String, Object> hashes = new HashMap<String, Object>();
hashes.put(str, helloObject);

What is more efficient from point of inserting and what from point of searching?


The first thing to get right should be correctness, not the efficiency: this code

HashMap<Integer, Object> hashes = new HashMap<Integer, Object>();
hashes.put(str.hashCode(), helloObject);

is incorrect (in addition to being inefficient *).

Recall that hash codes are not unique. The only requirement as per Java documentation is for hash codes of equal objects to be the same. However, objects with the same hash code are not necessarily equal. As the result, changing your hash map's key from String to Integer changes the semantics: two entirely different objects may be considered the same key absolutely arbitrarily, based on their hash code.

* In case you are curious why the above code snippet is inefficient, there is an autoboxing going on: hashCode() returns a primitive int, which is wrapped in a java.lang.Integer by the compiler. This often leads to creating an unwanted object in a situation where no additional object is created when you use a String.

Keep in mind that two strings with the same hash code might not be equal.

If you use the string's hashCode instead of the string itself then two different strings can produce the same map's key and that might result in a strange behavior.

Try this code and see what I mean.

Map<Integer, String> map = new HashMap<Integer, String>();
map.put("FB".hashCode(), "FB");
map.put("Ea".hashCode(), "Ea");


will output



"FB".hashCode() == "Ea".hashCode() // is true

Thus you should better use the String as the key.

You shouldn't care about the efficiency of this. What you need to care about is the use case you have. Use Integers and Strings where they are appropriate.

If you wish to understand how it works internally look at What issues should be considered when overriding equals and hashCode in Java?. It is basically all about the implementation of hashCode() and equals(). Please note that a key must also be immutable.

It depends. If you know that you will be only using it for Integers then just use Integer. If you plan on filling with all sorts of data/objects then use Object and probably some sort of a value resolver for it.

tl;dr: Is it going to be filled with only Integers? --> Define it as an Integer map, otherwise use Object.

Need Your Help

MVC, where do the classes go?

php model-view-controller oop

My understanding of the MVC is as follows (incase it's horribly wrong, I am afterall new to it)

How to increment number of rows in .csv

ios objective-c csv

I am doing a iPhone application in Xcode 5 where I read a list of president names from a .csv file. I also read other info such as party and date from-to in office from the file.