Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7781333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:11:42+00:00 2026-06-01T19:11:42+00:00

Let V be a class with a single attribute named K and its getter

  • 0

Let V be a class with a single attribute named K and its getter and setters.

What’s supposed to happen if I do:

V v = new V();
v.setK("a");
HashMap<K,V> map = new HashMap<K,V>();
map.put(v.getk(),v);
v.setK("b");

As far as I know, this should cause some kind of problem because a map key is supposed to be invariable. What would happen here?

Edit: Consider the key not to be a String but a mutable object as stated in the coment below.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T19:11:44+00:00Added an answer on June 1, 2026 at 7:11 pm

    The title of this question is misleading — you are not changing the map key, as in mutating the object used as map key. When you say map.put(x, y), you are creating a map entry that aggregates two independent values: a key and a value. Where the key originates from is not seen by the map, it’s just two objects. So, you have created a map entry ("a", v) and after that you just changed the state of v — there is no way this could have influenced the map entry’s key “a”. If, on the other hand, you had an object K of your own making, like

    public class K { 
      private String s;
      public K(String s) { this.s = s; }
      public void setS(String s) { this.s = s; }
      public boolean equals(Object o) { return ((K)o).s.equals(this.s); }
      public int hashCode() { return s.hashCode(); }
    

    }

    and now you do

    final K k = new K("a");
    map.put(k, v);
    k.setS("b");
    map.get(k);
    

    then you would face the problem — you mutated the object used as the map key.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to run two Servlet-class in a single web.xml but its not
Using Mongoid, let's say I have the following classes: class Map include Mongoid::Document embeds_many
I want to keep a single XmlDocument object in a class and let methods
Let's assume I want a single class Child and only this class to inherit
Okay, I'm creating a program from a single class, let's call it John class.
Pseudo-situation: have a class (let's say BackgroundMagic ), and it has Start() and Stop()
I have created a separate class (let's call it class2.cs for example) and want
I have one abstract class -let's say myBase. And I want all the classes
Suppose we are writing a class (let's call it Class) in an iPhone program.
I have a non-Activity class (let's call it NonActivity) that needs to post a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.