This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Websphere and the fly likes overriding hashcode and equals method internal work flow Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Products » Websphere
Reply Bookmark "overriding hashcode and equals method internal work flow" Watch "overriding hashcode and equals method internal work flow" New topic
Author

overriding hashcode and equals method internal work flow

jyothsna ananthula
Ranch Hand

Joined: Jun 02, 2011
Posts: 33
hi,
any body explain to me overriding hashcode and equals methods
in collections especially.

import java.util.HashSet;
import java.util.Set;

public class Employee {

int empid;
String empname;
static Employee e1, e2;

Employee(int id, String name) {
empid = id;
empname = name;
}

public static void main(String[] args) {
e1 = new Employee(12, "lod");
e2 = new Employee(12, "lod");


boolean a;
Set hs = new HashSet();
a = hs.add(e1);
System.out.println(a);
a = hs.add(e2);


System.out.println(a);
System.out.println(hs);

}
@Override
public int hashCode() {
//System.out.println("Hi");

return empid;
}


public boolean equals(Object o) {

//System.out.println("hi1");
if (e1.empid == e2.empid) {
return true;
} else {
return false;
}
}


}

please any one explain this hashcode ovveriding and equals method internally how it will excute. and also which method executes first, here i used only 2 elements if i want for more than 3 elements how to write.

hashcode ovveridden means unique hashcodes are providing based on body implemention and improves retriving the elements.

here i written in hashcode body return empid means based on empid it will return hashcode for that object or some other else.
if writing instead of empid, 55 what is the meaning of this.and getting output same. and also what is the workflow behind these.

please let me know as soon as possible
jagadeesh manne
Greenhorn

Joined: Jul 05, 2011
Posts: 1
when you call set.add() method it internally calls first equals method and compare present object hashcode with previous objects hashcode. by default for every object hashcode is different.so it will accept duplicates.

Java.util.HashSet

now if you dont want to allow duplicates in your hashset you have to over ride equals and hashcode. what it means duplicate object?
if object contains same values then it is duplicate.
in your example you override hashcode with id. but if id and name is equals then it is duplicate. so you have to override hashcode with every value of that object.
for your example

if you want to add more objects in set better to create separate bean class with setter and getter methods. and create object for that class and add values and add each object to set. and over ride hashcode and equals method


and java bean class


in this case i validated if object contains same values for id and name and designation that is duplicate.if you want you can validate using only name or id or designation

please visit my java site for more info
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: overriding hashcode and equals method internal work flow
 
Similar Threads
Set Duplicates
why they use hashCode() method in equals() method
HashMap entries with duplicate keys...
Set Duplicates
Using java.util.HashSet for Custom Class