import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeSet;
{
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Integer getAge()
{
return age;
}
public void setAge(Integer age)
{
this.age = age;
}
private String name;
private Integer age;
public EqualsTest()
{
this.name = "Ravinder";
this.age = 0;
}
public EqualsTest(String name, Integer age)
{
this.name = name;
this.age = age;
}
@Override
public String toString()
{
StringBuffer sb = new StringBuffer();
sb.append("My name is " + this.getName() + " having an age "
+ this.getAge());
return sb.toString();
}
public static void main(String p[])
{
EqualsTest equalsTest = new EqualsTest();
EqualsTest equalsTest1 = new EqualsTest("Ravinder1", 1);
EqualsTest equalsTest2 = new EqualsTest("Ravinder2", 2);
EqualsTest equalsTest3 = new EqualsTest("Ravinder3", 3);
EqualsTest equalsTest4 = new EqualsTest("Ravinder4", 4);
EqualsTest equalsTest5 = new EqualsTest("Ravinder5", 5);
EqualsTest equalsTest6 = new EqualsTest("Ravinder6", 6);
EqualsTest equalsTest7 = new EqualsTest("Ravinder7", 7);
EqualsTest equalsTest8 = new EqualsTest("Ravinder8", 8);
EqualsTest equalsTest9 = new EqualsTest("Ravinder9", 9);
// System.out.println(equalsTest);
// System.out.println(equalsTest1);
// System.out.println("Shallow comparasion ==>"
// + (equalsTest == equalsTest1));
// System.out.println("Deep comparasion ==>"
// + equalsTest.equals(equalsTest1));
// System.out.println(equalsTest.hashCode());
// System.out.println(equalsTest1.hashCode());
Set
hashSet.add(equalsTest);
hashSet.add(equalsTest1);
hashSet.add(equalsTest2);
hashSet.add(equalsTest3);
hashSet.add(equalsTest4);
hashSet.add(equalsTest5);
hashSet.add(equalsTest6);
hashSet.add(equalsTest7);
hashSet.add(equalsTest8);
hashSet.add(equalsTest9);
Set
linkedHashSet.add(equalsTest);
linkedHashSet.add(equalsTest1);
linkedHashSet.add(equalsTest2);
linkedHashSet.add(equalsTest3);
linkedHashSet.add(equalsTest4);
linkedHashSet.add(equalsTest5);
linkedHashSet.add(equalsTest6);
linkedHashSet.add(equalsTest7);
linkedHashSet.add(equalsTest8);
linkedHashSet.add(equalsTest9);
Set
treeSet.add(equalsTest);
treeSet.add(equalsTest1);
treeSet.add(equalsTest2);
treeSet.add(equalsTest3);
treeSet.add(equalsTest4);
treeSet.add(equalsTest5);
treeSet.add(equalsTest6);
treeSet.add(equalsTest7);
treeSet.add(equalsTest8);
treeSet.add(equalsTest9);
if (hashSet.contains(equalsTest))
{
System.out.println("The object already exist in the set");
}
else
{
hashSet.add(equalsTest);
System.out.println("The object already exist in the set");
}
System.out.println("The actual size of the hashset is---> "
+ hashSet.size());
System.out.println("The values in the hashset are -->"
+ hashSet.toString());
System.out.println("The actual size of the linkedHashSet is---> "
+ linkedHashSet.size());
System.out.println("The values in the linkedHashSet are -->"
+ linkedHashSet.toString());
System.out.println("The actual size of the treeSet is---> "
+ treeSet.size());
System.out.println("The values in the treeSet are -->"
+ treeSet.toString());
}
@Override
public int hashCode()
{
int hashcode = this.getAge().hashCode() + this.getName().hashCode();
return hashcode;
}
@Override
public boolean equals(Object obj)
{
boolean isEqual = false;
if (obj == null)
{
return false;
}
if (this.getClass() == obj.getClass())
{
EqualsTest equalsTest = (EqualsTest) obj;
if (this.getName().equals(equalsTest.getName())
&& (this.getAge().equals(equalsTest.getAge())))
{
return true;
}
}
return isEqual;
}
// this function is declare in Comparator interface, need to sort the
// collection, comparator instance must be given to TreeSet constructor.
@Override
public int compare(Object o1, Object o2)
{
EqualsTest equalsTestObj1 = (EqualsTest) o1;
EqualsTest equalsTestObj2 = (EqualsTest) o2;
if (equalsTestObj1.getAge() > equalsTestObj2.getAge())
{
return 1;
}
else if (equalsTestObj1.getAge() < equalsTestObj2.getAge())
{
return -1;
}
else
return 0;
}
}