public class Biz {
private int id;
private String name;
public Biz(int id,String name){
this.id=id;
this.name=name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//重写equals方法。
@Override
public boolean equals(Object obj) {
if(obj instanceof Biz){
if(((Biz) obj).getId()==this.id&&((Biz)obj).getName().equals(this.name)){
return true;
}
return false;
}
return false;
}
}
测试类:
public class Test2 {
/**
* @param args
*/
public static void main(String[] args) {
List bizs=new ArrayList();
Biz biz[]={new Biz(1,"a"),new Biz(2,"b"),new Biz(3,"c"),new Biz(1,"a"),new Biz(3,"c")};
for(int i=0;i
if(!bizs.contains(biz[i])){
bizs.add(biz[i]);
}
}
System.out.println();
System.out.println("_-_-_-_-_-_-_-_-_-_-");
for(Biz b:bizs){
System.out.print(b.getName()+" " );
}
}
}
运行结果:
_-_-_-_-_-_-_-_-_-_-
a b c