tags:

views:

407

answers:

6

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.

+1  A: 

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.

Jon Skeet
URL is perhaps a bad example of a key in a hash map. From the documentation, the hash code is based upon all the URL components relevant for URL comparison. It's slow (as it involves resolving the URL against the name server) so it's probably better to use a URI.That, of course, is a complete aside to this question!
Jeff Foster
Thanks - will fix to URI :)
Jon Skeet
+5  A: 

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:

Gregory Pakosz
And, of course, if you override hashCode() you also must override equals(), otherwise your hash map will potentially do some weird things.
Ash
Nope, only an object that implements **both** equals and hash (or none of both) should be used as key.
Adrian
@Adrian > it was implied as i'm linking to the other answer... however implementing both equals and hashcode with a dump hashcode implementation will bring you nowhere...
Gregory Pakosz
+5  A: 

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!

Adrian
+1  A: 

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.

Stephen C
+2  A: 

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)

sateesh
A: 

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.

irreputable