import java.util.*;
class ArrayListTest3
{
public static void main(String[] args)
{
Person p0=new Person("gekui",22);
Person p1=new Person("gekui",22);
Person p2=new Person("gekui",22);
Person p3=new Person("gekui",23);
Person p4=new Person("wf",22);
Person p5=new Person("gekui",22);
ArrayList al=new ArrayList();
al.add(p0);
al.add(p1);
al.add(p2);
al.add(p3);
al.add(p4);
al.add(p5);
sop(removeSameP(al));
}
public static List removeSameP(ArrayList<Person> al) { //把方法体换一下
ArrayList newAl = new ArrayList();
HashMap<String,Person> map=new HashMap<String,Person>();
for (int i = 0; i < al.size(); i++) {
Person p=map.get(al.get(i).getName()+al.get(i).getAge());
if(p ==null){
map.put(al.get(i).getName()+al.get(i).getAge(), al.get(i));
newAl.add(al.get(i));
}
}
return newAl;
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
class Person
{
private String name;
private int age;
Person(String name,int age)
{
this.name=name;
this.age=age;
}
public void setAge(int age)
{
this.age=age;
}
public void setName(String name)
{
this.name=name;
}
public int getAge()
{
return this.age;
}
public String getName()
{
return this.name;
}
public String toString()
{
return name+":"+age+;
}
}
|