removeAll() 失效重现
今天做一个批量删除的功能,我使用了 List.removeAll()这个方法,但是该代码执行前后,被操作的列表的 size 并没由发生改变。排查了一下,是因为两个列表中存储对象不同的原因。
实体类:
public class Bean {
private int id;
private String name;
private String address;
public Bean(int id, String name, String address) {
this.id = id;
this.name = name;
this.address = address;
}
}
构建场景:
ArrayList<Bean> allStudents = new ArrayList<>();
ArrayList<Bean> boyStudents = new ArrayList<>();
for (int i = 0; i < 10 ; i++) {
Bean bean = new Bean(i,"name is "+i,"address is "+i);
allStudents.add(bean);
}
for (int i = 0; i < 5 ; i++) {
Bean bean = new Bean(i,"name is "+i,"address is "+i);
boyStudents.add(bean);
}
System.out.println("allStudents.size()------before-------------->"+allStudents.size());
System.out.println("remove result : "+allStudents.removeAll(boyStudents));
System.out.println("allStudents.size()-------after-------------->"+allStudents.size());
输出结果是:
allStudents.size()------before-------------->10
remove result : false
allStudents.size()-------after-------------->10
但是,换 String 对象执行 removeAll() 竟然可以成功!
因为操作对象不同,这是一个很简单的原因,但是接下来要实验的另一个小例子,绝对让你非常吃惊,我们讲Bean 替换成 String 字符串试一下。
ArrayList<Bean> allStudents = new ArrayList<>();
ArrayList<Bean> boyStudents = new ArrayList<>();
for (int i = 0; i < 10 ; i++) {
Bean bean = new Bean(i,"name is "+i,"address is "+i);
allStudents.add(bean);
}
for (int i = 0; i < 5 ; i++) {
Bean bean = new Bean(i,"name is "+i,"address is "+i);
boyStudents.add(bean);
}
System.out.println("allStudents.size()------before-------------->"+allStudents.size());
System.out.println("remove result : "+allStudents.removeAll(boyStudents));
System.out.println("allStudents.size()-------after-------------->"+allStudents.size());
输出结果是 :
allStudents.size()------before-------------->10
remove result : true
allStudents.size()-------after-------------->5
揭开这一切的面纱
从打印结果很明白的看到,removeAll() 成功执行。String也是对象,为什么会这样?代码不会说谎,我们去源码中去寻找答案。
从源码中发现,ArrayList 执行 removeAll() 方法流程如下图所示:
通过控制变量法分析,很容易就聚焦到 equals()这个方法,这个方法是 Object 的方法,默认实现是比较对象在内存的地址。
public boolean equals(Object obj) {
return (this == obj);
}
再看一下 String 中 equals() 方法,重写了 Object 的这个方法,不再是比较地址,而是比较字符串是否相同。
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String) anObject;
int n = count;
if (n == anotherString.count) {
int i = 0;
while (n-- != 0) {
if (charAt(i) != anotherString.charAt(i))
return false;
i++;
}
return true;
}
}
return false;
}
这样的话,引发了一个思考,也就是说,如果自定义对象,重写 equals() 中的实现,也是可以实现非相同对象的情况下,成功 removeAll()的。这里我用 上面例子的 Bean 实体类简单实验一下:
public class Bean {
private int id;
private String name;
private String address;
public Bean(int id, String name, String address) {
this.id = id;
this.name = name;
this.address = address;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Bean bean = (Bean) o;
if (id != bean.id) return false;
if (!name.equals(bean.name)) return false;
return address.equals(bean.address);
}
@Override
public int hashCode() {
int result = id;
result = 31 * result + name.hashCode();
result = 31 * result + address.hashCode();
return result;
}
}
再次执行第一个例子的程序,ArrayList 成功 removeAll,打印信息如下:
allStudents.size()------before-------------->10
remove result : true
allStudents.size()-------after-------------->5