[英]set not able to remove the duplicates
I have a query that is I have develop a pojo and in that pojo I have customize the hashcode method and equals method as shown below in the pojo named Emp.java now my query is that i am adding the objects of that type in the set i have also develop the set seprately , rite now I haven't taken the advantage of generics later i will customize it but now I am adding the objects in set but in the result of set duplicate objects are shown , as ideally the unique objects must be shown please advise how can i overcome from this problem.. 我有一个查询,我已经开发了一个pojo,在那个pojo中,我自定义了hashcode方法和equals方法,如下所示在名为Emp.java的pojo中,现在我的查询是我正在集合中添加该类型的对象我还单独开发了集合,但现在还没有利用泛型的优势,我将对其进行自定义,但是现在我要在集合中添加对象,但是要显示集合重复对象的结果,理想情况下,唯一对象必须被显示请告知我该如何克服这个问题。
my pojo is .. 我的pojo是..
class Emp implements Comparable
{
String name,job;
int salary;
public Emp(String n,String j,int sal)
{
name=n;
job=j;
salary=sal;
}
public void display()
{
System.out.println(name+"\t"+job+"\t"+salary);
}
public boolean equals(Object o)
{
Emp p=(Emp)o;
return this.name.equals(p.name)&&this.job.equals(p.job)&&this.salary==p.salary;
}
public int hashCode()
{
return name.hashCode()+job.hashCode()+salary;
}
public int compareTo(Object o)
{
Emp e=(Emp)o;
return this.name.compareTo(e.name);
//return this.job.compareTo(e.job);
// return this.salary-e.salary;
}
}
and my set class is.. 我的课程是..
public class empset {
public static void main(String args[])
{
Set s1=new HashSet();
s1.add(new Emp("saral","coder",2300));
s1.add(new Emp("wer","der",2560));
s1.add(new Emp("Sachin","Programmer",24000));
s1.add(new Emp("Sachin","Programmer",24000));
System.out.println("There are "+s1.size()+" elements in the set.");
System.out.println("Content of set are : ");
Iterator itr=s1.iterator();
while(itr.hasNext())
{
Emp e=(Emp)itr.next();
System.out.print(e.hashCode()+"\t");
e.display();
}
}
upon execution the result i am getting is .. 执行后,我得到的结果是..
There are 4 elements in the set.
Content of set are :
15075785 wer der 2560
28921555 saral coder 2300
9209934 Sachin Programmer 24000
4766781 Sachin Programmer 24000
but ideally the set should remove the duplicates so the result i should get is .. 但理想情况下,该集合应该删除重复项,所以我应该得到的结果是..
There are 3 elements in the set.
Content of set are :
15075785 wer der 2560
28921555 saral coder 2300
9209934 Sachin Programmer 24000
as sachin name is duplicated and in set it should be taken as once 因为sachin名称是重复的,并且在设置中应该视为一次
I tried out your code, fixed some obvious compilation errors and it successfully removed the 4th element: 我尝试了您的代码,修复了一些明显的编译错误,并成功删除了第四个元素:
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class Empset
{
static class Emp implements Comparable
{
String name,job;
int salary;
public Emp(String n,String j,int sal)
{
name=n;
job=j;
salary=sal;
}
public void display()
{
System.out.println(name+"\t"+job+"\t"+salary);
}
public boolean equals(Object o)
{
Emp p=(Emp)o;
return this.name.equals(p.name)&&this.job.equals(p.job)&&this.salary==p.salary;
}
public int hashCode()
{
return name.hashCode()+job.hashCode()+salary;
}
public int compareTo(Object o)
{
Emp e=(Emp)o;
return this.name.compareTo(e.name);
}
}
public static void main(String args[])
{
Set s1=new HashSet();
s1.add(new Emp("saral","coder",2300));
s1.add(new Emp("wer","der",2560));
s1.add(new Emp("Sachin","Programmer",24000));
s1.add(new Emp("Sachin","Programmer",24000));
System.out.println("There are "+s1.size()+" elements in the set.");
System.out.println("Content of set are : ");
Iterator itr=s1.iterator();
while(itr.hasNext())
{
Emp e=(Emp)itr.next();
System.out.print(e.hashCode()+"\t");
e.display();
}
}
}
Result 结果
There are 3 elements in the set.
Content of set are :
199998062 Sachin Programmer 24000
219509 wer der 2560
204044336 saral coder 2300
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.