Rename Key in HashMap

Posted on 2005-04-23
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
    LVL 25

    Author Comment

    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??
    LVL 3

    Accepted Solution

    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


    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Fix45 challenge 15 66
    Understanding websocket example in spring 1 21
    Running Jira on Raspberry PI 2? 3 45
    Starting to learn JAVA, 7 35
    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…
    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now