Hi,
I had seen, only the String is used as a key in HashMap.Although the put() method takes Object as a parameter.What is the significant of it.If any other object can also used as a Key or not? Please provide the answers.
Hi,
I had seen, only the String is used as a key in HashMap.Although the put() method takes Object as a parameter.What is the significant of it.If any other object can also used as a Key or not? Please provide the answers.
You haven't shown which platform you're talking about, but I'll assume for the moment that it's Java.
You can use any type for the key in a HashMap
, but assuming you use the full generic version you need to specify that type and then use it consistent. For example, you could use URI as the key type:
HashMap<URI, Integer> hitCountMap = new HashMap<URI, Integer>();
The get
method takes Object as the key parameter type, but you should of course use the same type of key that you've put into the map. See this question for more discussion on that.
Any object that provides a meaningful implementation of hashCode()
is a perfect key candidate in a map: see Understanding the workings of equals and hashCode in a HashMap.
Also, as @Jon mentioned, all keys in your map should be of the same type.
EDIT: Of course, you need to implement both equals()
and hashcode()
. I thought the title of the link to the other question made that clear. But a dumb implementation of hashcode()
will just bring you a degenerate HashMap
which performance is poor.
EDIT2: As @Adrian mentioned in his answer, generics will help you constrain the type of keys and values for the map.
References:
A raw HashMap
will indeed accept any object as key. However, it is good style to specify which kind of keys and values you are going to use in a map
Map<String, Whatever> map = new HashMap<String, Whatever>();
if you do so, the put()
method will only accept strings.
NB: if you choose to use one of your own classes as keys, make sure the class either implements both equals
and hashCode
or none of them!
The key type can be any type, including (for some use-cases) Object. The only technical requirement is that equals(Object)
and hashcode()
are correctly implemented for all classes whose instances might be used as keys. In practice, you also want the semantics of equals(Object)
to be consistent with the intended behavior of the HashMap, across all possible key types / values.
However, if you genuinely do need to use Object as the key type, IdentityHashMap may be a better option. For a start, it doesn't use equals(Object)
or hashcode()
, but uses ==
and the key objects' "identity hash" values.
One probable reason why you see "String" used as hash key quite often is that it is an
immutable class.
Immutable objects make great map keys since one need not worry about their values
being modified once they're in a map or set, which would destroy
map or set's invaraints (Item 15 Effective Java, 2nd Edition)
In java web apps, programmers use way too many String objects everywhere. It's almost like programming in dynamic typed language. Prevailing usages of HashMap<String, XXX>
makes one wonder whether we are programming against javascript objects, in the hard way.