您的位置:首页 > 其它

list集合交集 并集 补集

2014-03-08 09:52 323 查看
前戏:


公司有个流程是这样的,有四条流程,一条流程四个节点。在每个流程第四个节点操作完后,复制当前节点的“疑点”数据至下一流程的节点。本来这是个很简单的功能,但是当上一条流程的节点数据更改后要同步到下一流程的首节点。原来的做法是删除下一节点的所以数据,重新插入上一节点的全部数据。当数据很大时就会相当耗费数据资源,所以引出了这篇文章。

注: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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: