pojo である employee というクラスがあり、この 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;
}
}
これが私の従業員タイプのリストです..
import java.util.*;
class EmpListDemo
{
public static void main(String arg[])
{
ArrayList list=new ArrayList();
list.add(new Emp("Ram","Trainer",34000));
list.add(new Emp("Sachin","Programmer",24000));
list.add(new Emp("Priyanka","Manager",54000));
list.add(1,new Emp("Ravi","Administrator",44000));
list.add(new Emp("Ram","Trainer",34000));
list.add(new Emp("Anupam","Programmer",34000));
list.add(new Emp("Sachin","Programmer",24000));
list.add(new Emp("Sachin","Team Leader",54000));
System.out.println("There are "+list.size()+" elements in the list.");
System.out.println("Content of list are : ");
ListIterator itr=list.listIterator();
while(itr.hasNext())
{
Emp e=(Emp)itr.next();
e.display();
}
}
}