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

  • SEARCH
  • Home
  • 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 8928823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:32:09+00:00 2026-06-15T08:32:09+00:00

I am trying to retrieve an object as a key in a Map when

  • 0

I am trying to retrieve an object as a key in a Map when I have changed an instance variable of it. As far as I am concerned, when I search a key object in a Map structure, it should come up when hashCode method returns the same value as initially. However, based on the following code, I cannot retrieve the object through method get() having altered the value of the variable weight:

public class Car  implements Comparable<Car> {

 int weight;
 String name;

 public Car(int w, String n) {
    weight=w;
    name=n;
 }

public boolean equals(Object o){
    if(o instanceof Car){
        Car d = (Car)o;
        return ((d.name.equals(name)) && (d.weight==weight));
    }
    return false;

}

public int hashCode(){
    return weight/2 + 17;
}

public String toString(){
    return "I am " +name+ " with weight: "+weight;
}


public int compareTo(Car d){
    if(this.weight>d.weight)
        return 1;
    else if(this.weight<d.weight)
        return -1;
    else
        return this.name.compareTo(d.name);
}

}


public static void main(String[] args) {
    Car d1 = new Car(107, "a");
    Car d2 = new Car(110, "b");
    Car d3 = new Car(110, "c");
    Car d4 = new Car(107, "a");

    Map<Car, Integer> m = new HashMap<Car, Integer>();
    m.put(d1, 1);
    m.put(d2, 2);
    m.put(d3, 3);
    m.put(d4, 16);

    System.out.println(m.get(d1).toString());
    d1.weight = 34;
    System.out.println(m.get(new Car(34, "a")));

    for(Map.Entry<Car, Integer> me : m.entrySet())
        System.out.println(me.getKey().toString() + " value: " +me.getValue());

}

The output is:

16

null

I am a with weight: 34 16

I am c with weight: 110 3

I am b with weight: 110 2

However, if I do not change the value of weight (omit the line: d1.weight = 34; ), merely using the line of code:

System.out.println(m.get(new Car(107, "a")));

the output is:

16

16

I am a with weight: 107 value: 16

I am c with weight: 110 value: 3

I am b with weight: 110 value: 2

It finds actually the object. Does it come to the expected output in both ways, or should it also in the first version have found and retrieved the object?

  • 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-15T08:32:10+00:00Added an answer on June 15, 2026 at 8:32 am

    You should be very careful when storing mutable objects as keys in a hashtable. The hash code is computed only once, on object insert. If you later change the object in a way that affects its hash code, it is very likely that it will be impossible to find.

    You need to remove the object and reinsert it whenever such a change is made.

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

Sidebar

Related Questions

I'm trying to retrieve a document when I have an object id - however,
I am trying to retrieve a child object based on the key in its
Here's what I'm trying to do: - Retrieve a Map of key-value pairs from
I'm trying to retrieve a user object from a foreign key reference but each
I´m trying to retrieve the id of the object I've just introduced into the
Hi I am trying to retrieve values from database. I have a row which
I have an stdClass Object like this: stdClass Object ( [key-west] => 1 [disney-land]
I am trying to retrieve values from Json Object but getting Exception.I am trying
In Java, I'm trying to retrieve a HashMap<String, Object> that has the Object which
I'm trying to retrieve the Manager (or Model) for a Django foreign key. This

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.