Results 1 to 6 of 6
  1. #1
    varun42 is offline Member
    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0

    Default 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!

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default 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.

  3. #3
    varun42 is offline Member
    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0

    Default Re: how hashtable and equals method has been called?

    Quote Originally Posted by masijade View Post
    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.


    But how only hashcode is printing while I am adding elements in the hashtable?

  4. #4
    varun42 is offline Member
    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0

    Default 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?

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default 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.

  6. #6
    varun42 is offline Member
    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0

    Default Re: how hashtable and equals method has been called?

    Quote Originally Posted by masijade View Post
    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.


    Thanks for the answer

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2013, 05:09 AM
  2. equals method :::::HELP:::::
    By alihht in forum New To Java
    Replies: 5
    Last Post: 03-09-2010, 08:19 AM
  3. ArrayLists compareTo method, equals method
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-26-2009, 08:20 PM
  4. equals method
    By mani_miit in forum Advanced Java
    Replies: 7
    Last Post: 09-09-2009, 11:26 PM
  5. Hashtable-put method
    By new_1 in forum New To Java
    Replies: 1
    Last Post: 12-23-2007, 08:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •