[1、差集( except )select a from t_aexceptselect a from t_b-- 也可写作:select a from t_a where a not in (select a from t_b)-- 多个字段时:select a,b from t_aexc
/**
* 求ls对ls2的差集,即ls中有,但ls2中没有的
*
* @param ls
* @param ls2
* @return
*/
public static List diff(List ls, List ls2) {
List list = new ArrayList(Arrays.asList(new Object[ls.size()]));
Collections.copy(list, ls);
list.removeAll(ls2);
return list;
}[ 说明:这里没有求差集的代码,有了交集和并集,差集=并集-交集 package com; import java.util.ArrayList; import java.util.HashSet; import java.u
/**
* 求2个集合的交集
*
* @param ls
* @param ls2
* @return
*/
public static List intersect(List ls, List ls2) {
List list = new ArrayList(Arrays.asList(new Object[ls.size()]));
Collections.copy(list, ls);
list.retainAll(ls2);
return list;
}
/**
* 求2个集合的并集
*
* @param ls
* @param ls2
* @return
*/
public static List union(List ls, List ls2) {
List list = new ArrayList(Arrays.asList(new Object[ls.size()]));
Collections.copy(list, ls);//将ls的值拷贝一份到list中
list.removeAll(ls2);
list.addAll(ls2);
return list;
}[在实现数据挖掘一些算法或者是利用空间向量模型来发现相似文档的时候,会遇到求两个集合的交集的情况。以前一般是采用比较笨拙的办法,依次遍历其中一个集 合的元素,然后