问题:无法移除2个集合中相同元素

方法:移除所包含的其所有元素。

在执行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方法

removeAll的更多相关文章

  1. 为IEnumerable<T>添加RemoveAll<IEnumerable<T>>扩展方法--高性能篇

    最近写代码,遇到一个问题,微软基于List<T>自带的方法是public bool Remove(T item);,可是有时候我们可能会用到诸如RemoveAll<IEnumerab ...

  2. List集合的removeAll(Collection<E> col) 和clear方法的区别

    //removeAll()方法private static void testList(){ List<String> list = new ArrayList<String> ...

  3. .NET清除Session 的几个方法[clear/removeAll/remove/Abandon]

    1.clear() 清空所有session对象的值,但保留会话   2.removeAll() 调用clear()方法   3.remove("SessionName") 删除某个 ...

  4. List.removeAll()方法失效

    List.removeAll()方法失效 前几天遇到List.removeAll()方法失效,测试了半天都没测出来,后面跟老大在那边调试了半天,最后终于找出原因,以后要是谁遇到这个奇葩的问题可以借鉴参 ...

  5. RemoveAll 要重写equals方法

    public class User { private String name; private int age; //setter and getter public String getName( ...

  6. C# .Net List<T>中Remove()、RemoveAt()、RemoveRange()、RemoveAll()的区别,List<T>删除汇总

    在List<T>中删除主要有Remove().RemoveAt().RemoveRange().RemoveAll()这几个方法.下面一一介绍使用方法和注意点. 我们以List<st ...

  7. JavaList addAll removeAll

    List<String>list1=new ArrayList<>(); list1.add("a"); list1.add("b"); ...

  8. List 的一个有用的高效的操作 removeAll

    如果有多个list集合,那么 使用 removeAll 可以快速的删除另外一个集合的内容: List<String> list1 = new ArrayList<String> ...

  9. list,set等集合遍历时,不能remove集合中的元素。需要new一个Object或者list,set,里面add需要删除的元素,等集合遍历完了进行remove(Object)或者removeAll(list/set)操作

    list,set等集合遍历时,不能remove集合中的元素.需要new一个Object或者list,set,里面add需要删除的元素,等集合遍历完了进行remove(Object)或者removeAl ...

随机推荐

  1. 关于NOMINMAX这个预处理宏

    标准库在<algorithm>头中定义了两个模板函数std::min() 和 std::max().通常用它可以计算某个值对的最小值和最大值.可惜在 Visual C++ 无法使用它们,因 ...

  2. HDU 5651 xiaoxin juju needs help

    组合数杨辉三角打表,这样避免了除法求逆元. #include<cstdio> #include<cstring> #include<cmath> #include& ...

  3. 在Android studio环境下使用EventBus

    EventBus是一个订阅/发布消息总线,实现在应用程序里面,组件之间,线程之间的通信.因为event是任意的类型,所以这个使用起来非常方便. eventbus中的角色: event:当然就是事件啦 ...

  4. coreGraphs和动画

    http://www.jianshu.com/p/b71c3d450e8e http://blog.csdn.net/volcan1987/article/details/9969455 http:/ ...

  5. FZU 1056 扫雷游戏

    水题.统计一下周围有几个雷. #include<cstdio> #include<cstring> #include<cmath> #include<algo ...

  6. Android L(5.0)源码之手势识别OnTouchListener

    在Activity中,因为要监听触摸屏的触摸事件和手势时间,所以该Activity必须实现OnTouchListener和OnGestureListener两个接口,并重写其中的方法.本人根据andr ...

  7. Led控件

    在 WindowMobile 上的模拟LED 显示屏插件 一个简单Led控件 一个经典的控制Led的单片机程序 Led控件(2)——Led显示屏模拟

  8. 总结分享十大iOS开发者最喜爱的库 分类: ios相关 app相关 2015-04-03 16:43 320人阅读 评论(0) 收藏

    该10大iOS开发者最喜爱的库由"iOS辅导团队"成员Marcelo Fabri组织投票选举而得,参与者包括开发者团队,iOS辅导团队以及行业嘉宾.每个团队都要根据以下规则选出五个 ...

  9. Hibernate的一些事儿

    一.Hibernate的工作原理: 1.读取并解析配置文件 2.读取并解析映射信息,创建SessionFactory 3.打开Sesssion 4.创建事务Transation 5.持久化操作 6.提 ...

  10. jquery textarea输入字符字数提示

    效果: html代码: <textarea id="assayInfo" name="assayInfo" rows="3" cols ...