Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Rename Key in HashMap

Posted on 2005-04-23
Medium Priority
Last Modified: 2012-06-21

I have a a HashMap, for the sake of this question, we'll say that it's like the following:

   HashMap map = new HashMap( 5, 0.75F );
   map.put( "KeyName", new JFrame("Example") );

I somehow, need to be able to rename the key "KeyName" to something else.. is there a way to change the 'Key' of an element in a HashMap??

Question by:InteractiveMind
  • 2
LVL 25

Author Comment

ID: 13849173
The only workaround for this that I can think of, would be:

   HashMap map = new HashMap( 5, 0.75F );
   map.put( "KeyName", new JFrame("Example") );
   Object temp = map.get( "KeyName" );
   map.remove( "KeyName" );
   map.put( "NewKeyName", temp );

However, this way doesn't seem very efficient.. :o\
Any other ideas??

Accepted Solution

RomanRega earned 2000 total points
ID: 13849212
In a HashMap if you change the key you actually move the object in the table.

It's the purpose of hashing doing so.
Hash means that the position of the object inside the table is calculated on the value of the key.
If that position is already occupied ("key collision"), the first position free in the table after that will be used instead.

The purpose, in fact, is to fast insert and fast access the object calculating thei position on the key.

So if you want to rename the kay, you have do extract from the table and then reinsert in a other position with an other key.

Anyway i think you can do so:

Object temp = map.remove( "KeyName" );
if (temp==null) {
     //the object is not there
     //do what you think is proper to do in this case for you application
}else map.put( "NewKeyName", temp );
LVL 25

Author Comment

ID: 13849219

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month13 days, 1 hour left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question