list集合交集 并集 补集
2014-03-08 09:52
399 查看
前戏:
公司有个流程是这样的,有四条流程,一条流程四个节点。在每个流程第四个节点操作完后,复制当前节点的“疑点”数据至下一流程的节点。本来这是个很简单的功能,但是当上一条流程的节点数据更改后要同步到下一流程的首节点。原来的做法是删除下一节点的所以数据,重新插入上一节点的全部数据。当数据很大时就会相当耗费数据资源,所以引出了这篇文章。
注:A与B分别代表整个
思路:listA(老的数据) 与 listB(新的数据),listA-listB的差集就是需要删除的数据。listB-listA的差集就是需要新增数据库的数据。
/** * @param args */ public static void main(String[] args) { List<User> listA = new ArrayList<User>(); // 原来的list List <User>listB = new ArrayList<User>(); // 新的list listA.removeAll(listB);
// 制造测试数据 for(int i=0;i<6;i++){ User u1 = new User(i,"张三"+i,String.valueOf(i)); listA.add(u1); if(i<5){ User u2 = new User("张三"+i,String.valueOf(i)); listB.add(u2); } }
// 输出原始数据 System.out.println("-----------------------listA-------------------------"); for(User u: listA){ System.out.println(u.toString()); } System.out.println("-----------------------listB-------------------------"); for(User u: listB){ System.out.println(u.toString()); } // 新增数据库的数据 List<User> newList = new ArrayList<User>(); newList.addAll(listB); newList.removeAll(listA);
// 删除数据库的数据 List<User> delList = new ArrayList<User>(); delList.addAll(listA); delList.removeAll(listB); System.out.println("-----------------------newlist-------------------------"); for(User u: newList){ System.out.println(u.toString()); } System.out.println("-----------------------dellist-------------------------"); for(User u: delList){ System.out.println(u.toString()); } }
}输出的效果没有达到预期,后来翻看源代码发现时
java.util
类 AbstractCollection<E>中的removeAll()方法中使用的contains()的问题。
/** * {@inheritDoc} * * <p>This implementation iterates over this collection, checking each * element returned by the iterator in turn to see if it's contained * in the specified collection. If it's so contained, it's removed from * this collection with the iterator's <tt>remove</tt> method. * * <p>Note that this implementation will throw an * <tt>UnsupportedOperationException</tt> if the iterator returned by the * <tt>iterator</tt> method does not implement the <tt>remove</tt> method * and this collection contains one or more elements in common with the * specified collection. * * @throws UnsupportedOperationException {@inheritDoc} * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} * * @see #remove(Object) * @see #contains(Object) */ public boolean removeAll(Collection<?> c) { boolean modified = false; Iterator<?> e = iterator(); while (e.hasNext()) { if (c.contains(e.next())) { e.remove(); modified = true; } } return modified; }
/** * {@inheritDoc} * * <p>This implementation iterates over the elements in the collection, * checking each element in turn for equality with the specified element. * * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ public boolean contains(Object o) { Iterator<E> e = iterator(); if (o==null) { while (e.hasNext()) if (e.next()==null) return true; } else { while (e.hasNext()) if (o.equals(e.next())) return true; } return false; }
contains(): 如果此 collection 包含指定的元素,则返回 true。 参考:http://hi.baidu.com/jiangyou001/item/90b5ude31fa2019493075a1ce
中用到了equals()方法,所以需要重写下equals()。参考:http://java.chinaitlab.com/base/814460.html
package com.bean; public class User { private int id; private String name; private String age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public User(int id, String name, String age) { super(); this.id = id; this.name = name; this.age = age; } public User(String name, String age) { super(); this.name = name; this.age = age; } public boolean equals(Object obj) { if((obj instanceof User) && obj!=null) { User u = (User)obj; //System.out.println("u.name:"+u.name+"this.name "+this.name); //System.out.println("u.age:"+u.age+"this.age "+this.age); if((u.name.equals(this.name)) && (u.age.equals(this.age))) { return true; } } return false; } @Override public String toString() { return "User [age=" + age + ", id=" + id + ", name=" + name + "]"; } }
交并补:点击打开链接
合并去重:/article/9592952.html
相关文章推荐
- 集合:求A、B两个集合的交集、并集和补集的代码(C语言)
- python 集合 并集、交集 Series list set 转换
- 集合遍历与删除,java list 交集 并集 差集 去重复并集
- 求两个集合的交集,补集,并集
- 集合中的(交集,并集,差集,补集,对称差集)
- 输入集合A、B和全集C,求两集合的交集、并集、补集、差集
- JAVA List集合取交集并集差集
- 求List集合的并集、交集、差集
- List集合的并集、交集、差集以及源码
- java求集合的并集、交集、补集、差集和对称差集
- grep和map计算两个集合交集、并集、补集
- python 集合 并集、交集 Series list set 转换的实例
- 取得集合list的交集并集(javaAPI或者apache的API)
- python 集合比较(交集、并集,差集)
- python两个 list 获取交集,并集,差集的方法
- 2 个 List 集合数据求并、补集操作
- list set 求交集、差集、并集
- js取两个数组的交集|差集|并集|补集|去重示例代码
- 计算集合的交集和并集
- java集合(交集,并集,差集)