removeAll() 失效重現(xiàn)
今天做一個批量刪除的功能滋迈,我使用了 List.removeAll()這個方法罪郊,但是該代碼執(zhí)行前后版确,被操作的列表的 size 并沒由發(fā)生改變扣囊。排查了一下,是因為兩個列表中存儲對象不同的原因绒疗。
實體類:
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 對象執(zhí)行 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() 成功執(zhí)行。String也是對象伟叛,為什么會這樣私痹?代碼不會說謊,我們去源碼中去尋找答案统刮。
從源碼中發(fā)現(xiàn)紊遵,ArrayList 執(zhí)行 removeAll() 方法流程如下圖所示:
通過控制變量法分析,很容易就聚焦到 equals()這個方法侥蒙,這個方法是 Object 的方法,默認實現(xiàn)是比較對象在內存的地址暗膜。
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;
}
這樣的話论衍,引發(fā)了一個思考瑞佩,也就是說,如果自定義對象饲齐,重寫 equals() 中的實現(xiàn)钉凌,也是可以實現(xiàn)非相同對象的情況下咧最,成功 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;
}
}
再次執(zhí)行第一個例子的程序,ArrayList 成功 removeAll矢沿,打印信息如下:
allStudents.size()------before-------------->10
remove result : true
allStudents.size()-------after-------------->5