GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Key in the HashMap. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Key in the HashMap." Watch "Key in the HashMap." New topic
Author

Key in the HashMap.

pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 410

Hi All,

I have hashmap..and keys are object to put in map..i need a new object ecah time I need to put a value. Is there any way i can resue the same object for different key?

Pawan Chopra
SCJP - DuMmIeS mInD
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
1) Can you clarify (greatly) just what you're trying to do?
2) Is this truly a Swing or other GUI-related question? And if so, how?

Much luck
pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 410

Oh Sorry...posted in wrong forum....Can any one please move it to Java general forum.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Oh Sorry...posted in wrong forum....Can any one please move it to Java general forum.

I think that this is the minor issue regarding your question and that the greater problem is that the question is still not comprehensible. Don't you want to fix this?
pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 410

Actually I have 10000 objects and I need to put hem in to a map and use them as Key. It may be 10 millions. My question is there any way I can save myself from creating so many objects ....I am worried about the performance.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14194
    
  20

When you use an object as a key in a HashMap, the object's state must not change as long as it is used as a key. If the object's state changes in a way so that the hash code of the object changes, then the HashMap will get confused and won't work properly anymore. It's safest to use only immutable objects as keys in a HashMap.

So, you cannot use a mutable object like this, for example:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
GeeCON Prague 2014
 
subject: Key in the HashMap.