在浏览器中有一个对象xml>看到这个标题,估计很多人会纳闷:连集合类的removeAll方法都用不好还当什么程序员。
好吧,我承认我确实没用好这个方法,鄙视我吧。O(∩_∩)O哈!
先贴问题--->
实体类(User):
[java] view
plaincopy
public class User {
private String name;
private int age;
//setter and getter
public String getName() {
return name;
}
public void setName(String name) {
thisname = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
thisage = age;
}
}
测试集合类(UserList):
[java] view
plaincopy
import javautilArrayList;
import javautilList;
public class UserList {
private List<User> subList;
private List<User> allList;
public UserList(){
subList=new ArrayList<User>();
allList=new ArrayList<User>();
for(int i=0;i<3;i++){
User user=new User();
usersetAge(i);
usersetName("lyh"+i);
subListadd(user);
}
for(int i=0;i<10;i++){
User user=new User();
usersetAge(i);
usersetName("lyh"+i);
allListadd(user);
}
}
public static void main(String[] args){
UserList userList=new UserList();
userListallListremoveAll(userListsubList);//调用removeAll方法
Systemoutprintln(userListallListsize());
}
}
诸君认为最后的打印的结果是多少? 7 ?That's wrong !! 结果是10。
为什么会这样哪?难道removeAll方法有问题?
这个就是最近在用到removeAll时遇到的疑问(当然,我是把实际中的问题简单的抽象出来了)。当时百思不得其解,甚至幼稚的以为是Java的BUG 。殊不知是自己脑袋BUG了 !
原因解析:
先看API
[xhtml] view
plaincopy
boolean removeAll(Collection<> c)
从列表中移除指定 collection 中包含的其所有元素(可选 *** 作)。
没错,就是移除子集合包含的元素,那为什么不是7,而是10 。
再看API说明,移除所包含的其所有元素,注意这个字眼:包含!
因为在执行removeAll方法时,会先对集合元素进行比较,如果元素相等才执行移除 *** 作,说到这,相信很多人都已经明白是怎么回事了,因为不相等(equals),所以没有执行移除。
查看源码进一步证实上述猜测,remove和removeAll的方法实现在:
javautilAbstractCollection<E>
具体代码为:
[java] view
plaincopy
public boolean removeAll(Collection<> c) {
boolean modified = false;
Iterator<> it = iterator();
while (ithasNext()) {
if (ccontains(itnext())) {
itremove();
modified = true;
}
}
return modified;
}
[java] view
plaincopy
public boolean remove(Object o) {
Iterator<E> it = iterator();
if (o==null) {
while (ithasNext()) {
if (itnext()==null) {
itremove();
return true;
}
}
} else {
while (ithasNext()) {
if (oequals(itnext())) {
itremove();
return true;
}
}
}
return false;
}
可以看到在调用removeAll方法时,实际上是循环调用了remove方法,而remove方法中有一段关键的代码:if (oequals(itnext()))
!
So,得出结论,因为上述例子中的实体类没有Override
hashCode和equals方法 !而在执行removeAll方法时是通过equals方法来判断集合元素是否相等的,如果没有Override
equals方法,其默认的仍是比较对象,所以会出现上述问题!
归根到底,还是基础没有掌握牢固,同时也给自己提了个醒,写实体类时尽量Override hashCode和equals方法,不这样说不定哪天就会出问题。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)