Hi friends my aim is avoid duplicate entries of Employee class Object.
If you add Employee objects like below to HashSet then it will allow.
I have class Employee:
class Employee
{
int eno;
String ename;
}
and code to add Employee Objects to HashMap:
Employee e1=new Employee(1,"jaffar");
Employee e2=new Employee(1,"jaffar");
Set<Employee> hs=new HashSet<Employee>();
hs.add(e1);
hs.add(e2);
That means we know that HashSet dosen't allow duplicate values, but e1,e2 object with sama data are added successfully. Because in this case HashSet only checks object addresses. Since e1,e2 has different addresses, it will allow u to add.
You may have a requirement where u need to avoid the duplicate elements to be added based on object content(like eno, ename). To achieve this requirement u should override equals() and hashCode() method in Employee class.
So that i have done this:
import java.util.*;
class Employee
{
int eno;
String ename;
public int getEno() {
return eno;
}
public void setEno(int eno) {
this.eno = eno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
Employee(int a,String n)
{
eno=a;
ename=n;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((ename == null) ? 0 : ename.hashCode());
result = prime * result + eno;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Employee))
return false;
Employee other = (Employee) obj;
if (ename == null) {
if (other.ename != null)
return false;
} else if (!ename.equals(other.ename))
return false;
if (eno != other.eno)
return false;
return true;
}
}
public class HashSetDuplicate {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee e1=new Employee(1,"jaffar");
Employee e2=new Employee(1,"shaik");
Set<Employee> hs=new HashSet<Employee>();
hs.add(e1);
hs.add(e2);
System.out.println("Successfully added elements = "+hs.size());
}
}
If you add Employee objects like below to HashSet then it will allow.
I have class Employee:
class Employee
{
int eno;
String ename;
}
and code to add Employee Objects to HashMap:
Employee e1=new Employee(1,"jaffar");
Employee e2=new Employee(1,"jaffar");
Set<Employee> hs=new HashSet<Employee>();
hs.add(e1);
hs.add(e2);
That means we know that HashSet dosen't allow duplicate values, but e1,e2 object with sama data are added successfully. Because in this case HashSet only checks object addresses. Since e1,e2 has different addresses, it will allow u to add.
You may have a requirement where u need to avoid the duplicate elements to be added based on object content(like eno, ename). To achieve this requirement u should override equals() and hashCode() method in Employee class.
So that i have done this:
import java.util.*;
class Employee
{
int eno;
String ename;
public int getEno() {
return eno;
}
public void setEno(int eno) {
this.eno = eno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
Employee(int a,String n)
{
eno=a;
ename=n;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((ename == null) ? 0 : ename.hashCode());
result = prime * result + eno;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Employee))
return false;
Employee other = (Employee) obj;
if (ename == null) {
if (other.ename != null)
return false;
} else if (!ename.equals(other.ename))
return false;
if (eno != other.eno)
return false;
return true;
}
}
public class HashSetDuplicate {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee e1=new Employee(1,"jaffar");
Employee e2=new Employee(1,"shaik");
Set<Employee> hs=new HashSet<Employee>();
hs.add(e1);
hs.add(e2);
System.out.println("Successfully added elements = "+hs.size());
}
}
and out put is:
Successfully added elements = 2
If you observe above code in equals method i have logic for both eno and ename to avoid duplicate eno and ename. And Red colored code i have enter duplicate eno's.
Expected output is: Successfully added elements = 1
Resulted output is: Successfully added elements = 2
My doubt is why it give like that.