容器:这个在spring中很重要,我们写的对象如何remove呢?写一小程序吧
现在看看接口吧。
set无序不可重复,list有序可重复,map为Key-Value
Collection c=new HashSet();
c.add("hello");
c.add(new Integer(100));
c.add(new Name("ff","f1"));
c.remove("hello");
c.remove(new Name("ff","f1"));
name为对象。属性为name,sex。是否可以被删除么?
答案不可以。那么如何才能删除呢?重写equals和hashcode方法就哦了。
public int hashCode()
{
return name.hashCode();
}
public boolean equals(Object obj)
{
if(obj instanceof Name)
{
Name aa=(Name)obj;
return name.equals(aa.name)&&sex.equals(aa.sex);
}
return super.equals(obj);
}
嘿嘿这样就可以了。
自己写程序时候往往忽略了这一点哦。