removeAll
2015-09-10 12:43
295 查看
问题:无法移除2个集合中相同元素
方法:移除所包含的其所有元素。
在执行removeAll方法时,会先对集合元素进行比较,如果元素相等才执行移除操作,说到这,相信很多人都已经明白是怎么回事了,因为不相等(equals),所以没有执行移除。
java.util.AbstractCollection<E>
removeAll
remove
注:if (o.equals(it.next())) !
上述例子中的实体类没有Override hashCode和equals方法
方法:移除所包含的其所有元素。
在执行removeAll方法时,会先对集合元素进行比较,如果元素相等才执行移除操作,说到这,相信很多人都已经明白是怎么回事了,因为不相等(equals),所以没有执行移除。
java.util.AbstractCollection<E>
removeAll
public boolean removeAll(Collection<?> c) { boolean modified = false; Iterator<?> it = iterator(); while (it.hasNext()) { if (c.contains(it.next())) { it.remove(); modified = true; } } return modified; }
remove
public boolean remove(Object o) { Iterator<E> it = iterator(); if (o==null) { while (it.hasNext()) { if (it.next()==null) { it.remove(); return true; } } } else { while (it.hasNext()) { if (o.equals(it.next())) { it.remove(); return true; } } } return false; }
注:if (o.equals(it.next())) !
上述例子中的实体类没有Override hashCode和equals方法
相关文章推荐
- 使用CodeMaid自动程序排版[转]
- 百度云管家开始耍流氓了
- storm操作语句.docx
- EasyCodeScanner导入Xcode报错
- Hacker之路技能树(1)
- 回望英语-3
- 零基础学python-15.1 为什么需要编写函数
- 零基础学python-15.1 为什么需要编写函数
- MsgBox-官方文档
- 使用位运算计算两个整数的加减
- 前端环境安装
- 深入理解C# 静态类与非静态类、静态成员的区别
- Hibernate笔记——缓存机制详细分析
- 第二届云鼎奖“中国最具潜力企业奖”花落安畅
- 黑马程序员_java05_反射
- ML 徒手系列 拉格朗日乘子法
- Golang 常用字符串处理方法汇总
- eclipse中查看storm0.9.3示例代码storm-start-05.docx
- MySQL 最基本的SQL语法/语句
- canvas基本用法