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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:51:21+00:00 2026-06-04T05:51:21+00:00

Sorry … about this silly/stupid question, guys: Why aren’t equals() and hashCode() being applied?

  • 0

Sorry … about this silly/stupid question, guys:

Why aren’t equals() and hashCode() being applied?

Currently they are only working as I expected for HashSet.

UPDATE

EVEN key value 5 is repeated but it doesn’t call equals and hashCode.

I want to apply it also on Value.

As just like HashSet calls equal and hashCode in this example, why hashMap is not being called equals and hashCode even if for key.

UPDATE2 – ANSWER

HashMap’s key(class->HashCode, equals) would be called.
Thank you All.
I was little bit confused on this.
🙂

    public class Employee {

        int id;
        String name; 
        int phone;

        public Employee(int id, String name, int phone) {
            this.id = id;
            this.name = name;
            this.phone = phone;
        }    
    // Getter Setter

        @Override
        public boolean equals(Object obj) {

            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Employee other = (Employee) obj;
            System.out.println("Employee -  equals" + other.getPhone());
            if (this.id != other.id) {
                return false;
            }
            if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
                return false;
            }
            if (this.phone != other.phone) {
                return false;
            }
            return true;
        }

        @Override
        public int hashCode() {
            System.out.println("Employee -  hashCode" );
            int hash = 3;
            hash = 67 * hash + this.id;
            hash = 67 * hash + (this.name != null ? this.name.hashCode() : 0);
            hash = 67 * hash + this.phone;
            return hash;
        }
    }

____________________________________________________________________________________

public class MapClass {

    public static void main(String[] args) {
        Map<Integer,Employee> map = new HashMap<Integer,Employee>();
        map.put(1, new Employee(1, "emp", 981));
        map.put(2, new Employee(2, "emp2", 982));
        map.put(3, new Employee(3, "emp3", 983));
        map.put(4, new Employee(4, "emp4", 984));
        map.put(5, new Employee(4, "emp4", 984));
       **//UPDATE**
        map.put(5, new Employee(4, "emp4", 984));            

        System.out.println("Finish Map" + map.size());
        Set<Employee> set = new HashSet<Employee>();

        set.add(new Employee(1, "emp", 981));
        set.add(new Employee(2, "emp2", 982));
        set.add(new Employee(2, "emp2", 982));
        set.add(new Employee(3, "emp3", 983));
        set.add(new Employee(4, "emp4", 984));
        set.add(new Employee(4, "emp4", 984));

        System.out.println(set.size());
    }
}

OUTPUT IS

Finish Map5
Employee -  hashCode
Employee -  hashCode
Employee -  hashCode
Employee -  equals982
Employee -  equals982
Employee -  hashCode
Employee -  hashCode
Employee -  hashCode
Employee -  equals984
Employee -  equals984
4
  • 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-04T05:51:22+00:00Added an answer on June 4, 2026 at 5:51 am

    EVEN key value 5 is repeated but it doesn’t call equals and hashCode

    Yes it does call hashCode, on the key, the Integer.

    I want to apply it also on Value

    Dose of reality: Java HashMaps don’t work that way. They check the key only for duplicates, not the value, and this is as it should be.

    If you want Employee’s hash to be checked in the Map, then it must be the key. Period.

    Another possible solution is to download one of the multimaps that are available.

    Edit to see that it’s calling hashCode and equals, change your Map’s key type to something like so:

    class MyInt {
       private Integer i;
    
       public MyInt(Integer i) {
          this.i = i;
       }
    
       public Integer getI() {
          return i;
       }
    
       @Override
       public int hashCode() {
          System.out.println("MyInt HashCode: " + i.hashCode());
         return i.hashCode();
       }
    
       @Override
       public boolean equals(Object obj) {
          System.out.printf("MyInt equals: [%s, %s]%n", i, obj);
          if (this == obj)
             return true;
          if (obj == null)
             return false;
          if (getClass() != obj.getClass())
             return false;
          MyInt other = (MyInt) obj;
          if (i == null) {
             if (other.i != null)
                return false;
          } else if (!i.equals(other.i))
             return false;
          return true;
       }
    
       @Override
       public String toString() {
          return i.toString();
       }
    
    }
    

    Then fill your Map like so:

       Map<MyInt,Employee> map = new HashMap<MyInt,Employee>();
       map.put(new MyInt(1), new Employee(1, "emp", 981));
       map.put(new MyInt(2), new Employee(2, "emp2", 982));
       map.put(new MyInt(3), new Employee(3, "emp3", 983));
       map.put(new MyInt(4), new Employee(4, "emp4", 984));
       map.put(new MyInt(5), new Employee(4, "emp4", 984));
       map.put(new MyInt(5), new Employee(4, "emp4", 984));
    

    and you’ll see:

    MyInt HashCode: 1
    MyInt HashCode: 2
    MyInt HashCode: 3
    MyInt HashCode: 4
    MyInt HashCode: 5
    MyInt HashCode: 5
    MyInt equals: [5, 5]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry if this is a silly question, but I currently have the following and
Sorry if this is a stupid question... I've developed an application that creates absolute
Sorry for puting this silly question: I am reading the book Using R for
Sorry about this newbie question. Is there a way to execute multiple sentences at
Sorry for this not being a real question, but Sometime back i remember seeing
Sorry if this sounds like a really stupid question, but I need to make
Sorry if this is stupid question, but I'm new to MATLAB. I have a
Sorry, I'm sure this is a stupid question. I have successfully installed python 2.6
Sorry if this is a noob question, but All my code is error-free and
Sorry if this is a simple question, but I've only recently started learn coding

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.