Results 1 to 6 of 6
- 10-07-2013, 10:09 AM #1
Member
- Join Date
- Oct 2013
- Posts
- 4
- Rep Power
- 0
how hashtable and equals method has been called?
package com.java2novice.hashtable;
import java.util.Hashtable;
import java.util.Set;
public class MyHashtableDupEntry {
public static void main(String a[]){
Hashtable<Empl,String> tm = new Hashtable<Empl, String>();
tm.put(new Empl(134,"Ram",3000), "RAM");
tm.put(new Empl(235,"John",6000), "JOHN");
tm.put(new Empl(876,"Crish",2000), "CRISH");
tm.put(new Empl(512,"Tom",2400), "TOM");
System.out.println("Adding duplicate entry:");
tm.put(new Empl(512,"Tom",2400), "TOM");
System.out.println("Hashtable entries:");
Set<Empl> keys = tm.keySet();
for(Empl key:keys){
System.out.println(key+" ==> "+tm.get(key));
}
System.out.println("Duplicate got eliminated!!!");
}
}
class Empl{
private String name;
private int salary;
private int id;
public Empl(int id, String n, int s){
this.id = id;
this.name = n;
this.salary = s;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public String toString(){
return "Id: "+this.id+" -- Name: "+this.name+" -- Salary: "+this.salary;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
@Override
public int hashCode() {
System.out.println("In hashcode");
return this.getId();
}
@Override
public boolean equals(Object obj) {
Empl e = null;
if(obj instanceof Empl){
e = (Empl) obj;
}
System.out.println("In equals");
if(this.getId() == e.getId()){
return true;
} else {
return false;
}
}
}
Output:
In hashcode
In hashcode
In hashcode
In hashcode
Adding duplicate entry:
In hashcode
In equals
Hashtable entries:
In hashcode
In equals
Id: 876 -- Name: Crish -- Salary: 2000 ==> CRISH
In hashcode
In equals
Id: 512 -- Name: Tom -- Salary: 2400 ==> TOM
In hashcode
In equals
Id: 235 -- Name: John -- Salary: 6000 ==> JOHN
In hashcode
In equals
Id: 134 -- Name: Ram -- Salary: 3000 ==> RAM
Duplicate got eliminated!!!
Please let me know how "In hashcode " and "In equals" coming in the output!
- 10-07-2013, 10:21 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,568
- Rep Power
- 14
Re: how hashtable and equals method has been called?
When you add an entry to a HashMap, the Map must determine if the key already exists. It does this by assigning "buckets" on the value of the hashcode (hence a call to hashcode), then, when an object already exists in the "bucket" to which this one is to be added, it must determine if the they are equal, hence the call to equals, and thus the reason why you must ALWAYS override both the hascode AND equals methods of Object, if Object's implementations of these are not sufficient.
- 10-07-2013, 11:26 AM #3
Member
- Join Date
- Oct 2013
- Posts
- 4
- Rep Power
- 0
- 10-07-2013, 11:28 AM #4
Member
- Join Date
- Oct 2013
- Posts
- 4
- Rep Power
- 0
Re: how hashtable and equals method has been called?
I would like to repeat the question
At the start of output ,only "in hashcode " is printing not "in equals "
Why is it so?
- 10-07-2013, 11:34 AM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,568
- Rep Power
- 14
Re: how hashtable and equals method has been called?
I ALREADY told you. It is because when the object to add has a hashcode that,as yet, does not exist as a key in the map there is no reason to call equals. equals will only be called when the hashcode call produces a value that already exists in the map.
- 10-07-2013, 11:59 AM #6
Member
- Join Date
- Oct 2013
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
How to have the subclass method be called instead of the superclass method
By Randron in forum New To JavaReplies: 2Last Post: 07-11-2013, 05:09 AM -
equals method :::::HELP:::::
By alihht in forum New To JavaReplies: 5Last Post: 03-09-2010, 08:19 AM -
ArrayLists compareTo method, equals method
By random0munky in forum New To JavaReplies: 2Last Post: 10-26-2009, 08:20 PM -
equals method
By mani_miit in forum Advanced JavaReplies: 7Last Post: 09-09-2009, 11:26 PM -
Hashtable-put method
By new_1 in forum New To JavaReplies: 1Last Post: 12-23-2007, 08:07 PM
Bookmarks