我有下面这两个类..
class Emp //implements Comparable
{
String name,job;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
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;
}*/
}
另一个是..
import java.util.*;
class EmpHsDemo
{
public static void main(String arg[])
{
HashSet set=new HashSet();
set.add(new Emp("Ram","Trainer",34000));
set.add(new Emp("Ram","Trainer",34000));
set.add(new Emp("Ravi","Administrator",44000));
set.add(new Emp("Sachin","Programmer",24000));
set.add(new Emp("Priyanka","Manager",54000));
set.add(new Emp("Anupam","Programmer",34000));
set.add(new Emp("Sachin","Team Leader",54000));
System.out.println("There are "+set.size()+" elements in the set.");
System.out.println("Content of set are : ");
Iterator itr=set.iterator();
while(itr.hasNext())
{
Emp e=(Emp)itr.next();
System.out.print(e.hashCode()+"\t");
e.display();
}
System.out.println("**********************************");
Emp e1=new Emp("Ravi","Administrator",44000);
System.out.println("Removing following Emp from the set...");
System.out.print(e1.hashCode()+"\t");
e1.display();
set.remove(e1);
System.out.println("No. of elements after removal "+set.size());
/* Emp e2=new Emp("Anupam","Programmer",34000);
System.out.println("Searching following Emp in the set...");
System.out.print(e2.hashCode()+"\t");
e2.display();
System.out.println("Results of searching is : "+set.contains(e2));*/
}
}
现在我正在做一项研究
- 如果我评论 hashcode() 方法而不是评论 equals () 方法,它将使用它允许重复,因为 Ram 与内存地址一起显示两次,我得到以下结果..
There are 7 elements in the set.
Content of set are :
374283533 Priyanka Manager 54000
1660364311 Ram Trainer 34000
1340465859 Ravi Administrator 44000
2106235183 Sachin Programmer 24000
2031692173 Ram Trainer 34000
603737068 Anupam Programmer 34000
148669801 Sachin Team Leader 54000
**********************************
Removing following Emp from the set...
1807500377 Ravi Administrator 44000
No. of elements after removal 7
2. 如果我取消注释 hashcode() 方法和 equals () 方法,我会得到这个结果
There are 6 elements in the set.
Content of set are :
1546622676 Sachin Team Leader 54000
-302767206 Anupam Programmer 34000
149315535 Ravi Administrator 44000
199998062 Sachin Programmer 24000
1407883922 Priyanka Manager 54000
597555555 Ram Trainer 34000
**********************************
Removing following Emp from the set...
149315535 Ravi Administrator 44000
No. of elements after removal 5
3. 如果我只评论 equals() 方法而不是 hashcode() 那么我得到以下结果
There are 7 elements in the set.
Content of set are :
1546622676 Sachin Team Leader 54000
-302767206 Anupam Programmer 34000
149315535 Ravi Administrator 44000
199998062 Sachin Programmer 24000
1407883922 Priyanka Manager 54000
597555555 Ram Trainer 34000
597555555 Ram Trainer 34000
**********************************
Removing following Emp from the set...
149315535 Ravi Administrator 44000
No. of elements after removal 7
请告知这三种方法背后的原因是什么..!