Note that there are some explanatory texts on larger screens.

plurals
  1. POHashMap with override equals and hashCode not working
    primarykey
    data
    text
    <p>Sorry ... about this silly/stupid question, guys:</p> <p>Why aren't <code>equals()</code> and <code>hashCode()</code> being applied?</p> <p>Currently they are only working as I expected for <code>HashSet</code>.</p> <p><strong>UPDATE</strong></p> <p>EVEN key value 5 is repeated but it doesn't call equals and hashCode.</p> <p>I want to apply it also on Value.</p> <p>As just like HashSet calls equal and hashCode in this example, why hashMap is not being called equals and hashCode even if for key.</p> <p><strong>UPDATE2 - ANSWER</strong></p> <p>HashMap's key(class->HashCode, equals) would be called. Thank you All. I was little bit confused on this. :)</p> <pre><code> 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&lt;Integer,Employee&gt; map = new HashMap&lt;Integer,Employee&gt;(); 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&lt;Employee&gt; set = new HashSet&lt;Employee&gt;(); 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()); } } </code></pre> <p>OUTPUT IS </p> <pre><code>Finish Map5 Employee - hashCode Employee - hashCode Employee - hashCode Employee - equals982 Employee - equals982 Employee - hashCode Employee - hashCode Employee - hashCode Employee - equals984 Employee - equals984 4 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload