-2

I have a Test class and its objects with same value (101,"abc") is created twice. I am inserting these two object in Hashmap as a Key. I want to understand the internal functioning as to why I am getting size as two of a map when both of my keys are same, it should probably overwrite ?

import java.util.HashMap;
import java.util.Map;
public class Test{
int id;
String name;
public static void main(String[] args) {
Test e1 = new Test(101,"abc");
Test e2 = new Test(101,"abc");
//Test e3 = new Test(101,"abc");
Map<Test, String> map = new HashMap<>();
map.put(e1, "XYZ");
map.put(e2, "CDF");
String value = map.get(e2);
System.out.println( "VALUE : "+value);
}
public Test(int id, String name) {
this.id = id;
this.name=name;
}}
solveit
  • 869
  • 2
  • 12
  • 32
  • 1
    Did you ever hear about indentation? Makes every program much easier to read. – Henry Oct 11 '20 at 08:33
  • 2
    override `hashCode()` and `equals()` in your `Test` class. – Eran Oct 11 '20 at 08:34
  • 2
    Does this answer your question? [Understanding the workings of equals and hashCode in a HashMap](https://stackoverflow.com/questions/1894377/understanding-the-workings-of-equals-and-hashcode-in-a-hashmap) – martinspielmann Oct 11 '20 at 08:36

2 Answers2

1
Test e1 = new Test(101,"abc");
Test e2 = new Test(101,"abc");

Creates 2 different objects of type Test. This means 2 different memory space has been allocated for e1 & e2.

Now lets understand how does map( say Hash Map) identifies the uniqueness of key( in much simpler words, how does the map knows that the key you are trying to insert is already present or not). Answer is, map calls the hashcode() & equals() method to compare the keys present in map with the one you are trying to insert.

As we already know all classes have a default parent class Object . And the Test class is not having an implementation of hashcode() & equals(); so when map tried calling them, the object class method were called. As Object's class equals returns true only when both the objects in comparison refers to the same object reference & so does the hashcode() and here e1 & e2 are clearly different objects. So you got two entries.

Solution is to have an override the equals() & hashcode() as suggested by @Kapil above.

saiba sheikh
  • 126
  • 1
  • 2
0

There will be two objects in the hashmap unless you override hashcode() and equals() methods in your Test class. If you override these two methods on the id property of Test class then the two keys with the same id would be treated as duplicate and you will see the expected behaviour.

Example below states that you want to make two object equals if their ids are equal -

@Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + id;
            return result;
        }
        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Test other = (Test) obj;
            if (id != other.id)
                return false;
            return true;
        }