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 8601235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:47:45+00:00 2026-06-12T01:47:45+00:00

I had a very basic doubt with respect to Java HashMap when duplicate keys

  • 0

I had a very basic doubt with respect to Java HashMap when duplicate keys are being inserted.

My intention is to create 4 Emp objects. 2 Objects (e1 and e2) have the same hashCode. So when inserting the e1 (which is being inserted after e2), hashmap would realize that an object with the same hash value is already present (object e2). It will then compare the keys of all objects in the slot with the same hash value. If it finds an object with matching key (by calling the equals method of the Emp class below), it will replace the old value with the new one.

Please have a look at the test code below:

import java.util.Map;
import java.util.HashMap;
import java.util.Set;

class Emp {
        String name;
        int age;

        public Emp(String name, int age) {
                this.name = name;
                this.age = age;
        }

        public boolean equals(Object s) {
                if(s instanceof Emp) {
                        Emp s1 = (Emp) s;
                        return ((s1.name.compareToIgnoreCase(this.name) == 0));
                }
                return false;
        }

        public int hashCode() {
                //return (this.name.hashCode() + this.age);
                return this.name.hashCode();
        }
}

public class HashTest {
        public static void main(String[] args) {
                Emp e1 = new Emp("Terry", 26);
                Emp e2 = new Emp("Terry" , 60);
                Emp e3 = new Emp("John", 21);
                Emp e4 = new Emp("Test", 60);

                Map<Emp,Emp> emp = new HashMap<Emp, Emp>();
                emp.put(e2,e2);
                Emp v2 = emp.put(e1,e1);
                emp.put(e3,e3);
                emp.put(e4,e4);

                System.out.println("Replaced Record Name: " + v2.name + " , age: " + v2.age);
                for(Emp e: emp.keySet())
                        System.out.println("Name: " + e.name + " , age: " + e.age);
        }
}

The output I was expecting:
Replaced Record Name: Terry , age: 60
Name: Test , age: 60
Name: Terry , age: 26
Name: John , age: 21

The output I got:
Replaced Record Name: Terry , age: 60
Name: Test , age: 60
Name: Terry , age: 60
Name: John , age: 21

I was expecting (Terry, 60) to be replaced by (Terry, 26) object. This seems to be happening as i get Replaced Record Name: Terry , age: 60 as output. However, the map contains the record Name: Terry , age: 60 instead of Name: Terry , age: 26.

Edit: Thanks everyone for the suggestion. It turns out I was making a very careless mistake. Instead of printing the values associated with a key, I was printing only the key.

As everyone has pointed out, the solution is to:

  for(Emp e: emp.keySet())
  {
        Emp empVal = emp.get(e);
        System.out.println("Name: " + empVal.name + " , age: " + empVal.age);
  }
  • 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-12T01:47:47+00:00Added an answer on June 12, 2026 at 1:47 am

    Your output is printing the keys, not the values. In your code the key will not change but the value will.

    For instance, if you change your output loop to:

    for (Emp emp : emp.values()) {
      System.out.println("Name: " + e.name + " , age: " + e.age);
    }
    

    I suspect you’ll see the answer you were expecting.

    However: In general I would advise against doing what you’re doing here. All kinds of code expects that if a.equals(b) then a and b don’t have any meaningful differences at all, and your Emp class’s equals implementation doesn’t live up to that contract. For instance, if you used a HashSet rather than a HashMap you would get much more peculiar behavior and there’d be no way to fix it.

    A better way to implement this might be to have Emp‘s hashCode and equals methods properly respect name and age, and make your map be a Map<String, Emp> where the key is the employee’s name and the value is the Emp record.

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

Sidebar

Related Questions

So I had to create this splash page in a very short amount of
For an assignment, we had to implement something like a very basic sexp parser,
I have just create a very basic Dynamic Data web application using Entity Framework,
Overview I am trying to create a very basic scraper with PhantomJS and pjscrape
I have recently started looking into MQs and I had some very basic questions,
I have a very basic ListView in android and had set a very basic
I have a very basic ListView in android and had set a very basic
When we initially setup version control we had very limited time and only basic
I'm building a solution for a client which allows them to create very basic
I am creating a scrollview dynamically and I had a very basic question. Is

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.