4
A a = new A();     //classA {   }

HashMap<String, Object> hm = new Hashmap<String,Object>();

hm.put("A", a);

My question is, How can i put the Object itself instead of "A" in same declaration?

hm.put(`a??`, a);
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
user1010399
  • 2,258
  • 5
  • 30
  • 42
  • It makes little sense to have a hashmap's key be the same as its value. – Hovercraft Full Of Eels Sep 30 '12 at 18:37
  • You *can* write `hm.put(a, a);` if you declare your map with the proper generic types. – assylias Sep 30 '12 at 18:37
  • 4
    What are you trying to achieve, why do you want to do that? – Konrad Reiche Sep 30 '12 at 18:39
  • @platzhirsch: just wanted to know if we can do somehow. just an interview question in which i got confused. – user1010399 Sep 30 '12 at 18:41
  • 3
    @user1010399: Your question doesn't really make sense. – SLaks Sep 30 '12 at 18:41
  • 1
    @user1010399 Okay, I have added some information to that in my question. My guess is, the interviewer wanted to see whether you understood the Generic Type Parameters in Java. – Konrad Reiche Sep 30 '12 at 18:44
  • 1
    It could be done using `hm.put(a.toString(), a)`, but remember that every object you will add in your map must override the `toString` (or it will use the ugly `Object#toString`) method and you should store every String in some place. IMHO, this is silly and it shouldn't be used in real code. – Luiggi Mendoza Sep 30 '12 at 18:45
  • @platzhirsch: you are absolutely right and his very next question was about `generic`. may be he wanted to judge something else ;( – user1010399 Sep 30 '12 at 18:49

5 Answers5

3

You simply cannot do that, the language prohibits it. It would only be possible if your class A is a subclass of String which is not possible, since String is declared as final in Java.

With respect to you interview question: It's not possible due to the generic type parameter that was chosen for the declaration. You can read more about that in Bounded Type Parameters.

Konrad Reiche
  • 27,743
  • 15
  • 106
  • 143
  • i totally agree with you but interviewer told me it can be done but you have to use some (dirty)tricks while he didn't tell me about that. – user1010399 Sep 30 '12 at 18:46
1
A a = new A();     //classA {   }

Map<A, A> hm = new Hashmap<A, A>();

hm.put(a, a);

But I do not see any point of putting a->a

Elbek
  • 3,434
  • 6
  • 37
  • 49
0

If the class held a non-changing decent String field, you could use that.

// the id property must be a String, immutable and unique for each instance!
myMap.put(a.getId(), a);
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

If you want to make any object as a key in your HashMap, then that object has to be immutable.. Because, you don't want anyone to change your key, after you add them to your HashMap..

Just imagine, if your keys are changed after insertion, you won't ever be able to find your inserted value..

But if your key is immutable, then if anyone tries to change your keys, he will actually create a new one for himself, but you will still have yours..

That is what happens in case you use String as your key in HashMap(They can't be changed).. So, if you want your object to be a key, either you make your class a subclass of String (that you can't do), or, just make your class immutable..

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
0

This is actually possible using a raw type, like this:

Object key = ...;
Object value = ...;
Map<String, Integer> map = new HashMap<>();//a normal map
Map rawMap = map; // here is the raw type
rawMap.put(key, value); // it works!

This runs fine, but problems arise when you try to use the generic map later:

Integer value = map.get(key);// ClassCastException (unless value actually is an Integer)

That's why you were told that it's a "dirty trick". You shouldn't use it.

Community
  • 1
  • 1
ElectronWill
  • 754
  • 9
  • 17