public static void main(String[] args) {
// TODO Auto-generated method stub
Person p1=new Person("lisi1",21);
Person p2=new Person("lisi2",22);
Person p3=new Person("lisi3",23);
Person p4=new Person("lisi4",24);
Person p5=new Person("lisi3",23);
ArrayList list=new ArrayList();
list.add(p1);
list.add(p2);
list.add(p3);
list.add(p4);
list.add(p5);
list=getSingleElements(list);
Iterator it=list.iterator();
while(it.hasNext()){
Object obj=it.next();
Person p=(Person)obj;
System.out.println(p.getName()+"::"+p.getAge());
}
}
}
package zhangli1.bean;
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String toString()
{
return this.toString();
}
public boolean equals(Object obj){
if(this==obj){
return true;
}
if(!(obj instanceof Person))throw new ClassCastException("类型转换错误");
{
Person p=(Person)obj;
return this.name.equals(p.name)&&this.age==p.age;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Person p1=new Person("lisi1",21);
Person p2=new Person("lisi2",22);
Person p3=new Person("lisi3",23);
Person p4=new Person("lisi4",24);
Person p5=new Person("lisi3",23);
ArrayList list=new ArrayList();
list.add(p1);
list.add(p2);
list.add(p3);
list.add(p4);
list.add(p5);
list=getSingleElements(list);
Iterator it=list.iterator();
while(it.hasNext()){
Object obj=it.next();
Person p=(Person)obj;
System.out.println(p.getName()+"::"+p.getAge());
}
}
}
package zhangli1.bean;
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String toString()
{
return this.toString();
}
public boolean equals(Object obj){
if(this==obj){
return true;
}
if(!(obj instanceof Person))throw new ClassCastException("类型转换错误");
{
Person p=(Person)obj;
return this.name.equals(p.name)&&this.age==p.age;
}
}