JAVA获取两个List的交集、差集以及并集
import java.util.Set;
import java.util.List;
import java.util.HashSet;
import java.util.TreeSet;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.LinkedList;
public class getSet {
public static void main(String args[]) {
getList();
}
// 获取两个ArrayList的差集、交集、去重并集(数据量⼤⼩不限制)
private static void getList() {
List<String> firstArrayList = new ArrayList<String>();
List<String> secondArrayList = new ArrayList<String>();
List<String> defectList = new ArrayList<String>();//差集List
小白学java有前途吗List<String> collectionList = new ArrayList<String>();//交集List
List<String> unionList = new ArrayList<String>();//去重并集List
try {
firstArrayList.add("aaa");
firstArrayList.add("bbb");
firstArrayList.add("ccc");
firstArrayList.add("ddd");
secondArrayList.add("bbb");
secondArrayList.add("ccc");
secondArrayList.add("eee");
// 获取差集
defectList = receiveDefectList(firstArrayList, secondArrayList);
Iterator<String> defectIterator = defectList.iterator();
System.out.println("===================差集===================");
while(defectIterator.hasNext()) {
System.out.());
}
// 获取交集
collectionList = receiveCollectionList(firstArrayList, secondArrayList);
Iterator<String> collectionIterator = collectionList.iterator();
System.out.println("===================交集===================");
while(collectionIterator.hasNext()) {
System.out.());
}
// 获取去重并集
unionList = receiveUnionList(firstArrayList, secondArrayList);
Iterator<String> unionIterator = unionList.iterator();
System.out.println("===================去重并集===================");
while(unionIterator.hasNext()) {
System.out.());
}
}catch(Exception e) {
e.printStackTrace();
}
}
/**
* @⽅法描述:获取两个ArrayList的差集
* @param firstArrayList 第⼀个ArrayList
* @param secondArrayList 第⼆个ArrayList
* @param secondArrayList 第⼆个ArrayList
* @return resultList 差集ArrayList
*/
public static List<String> receiveDefectList(List<String> firstArrayList, List<String> secondArrayList) {
List<String> resultList = new ArrayList<String>();
LinkedList<String> result = new LinkedList<String>(firstArrayList);// ⼤集合⽤linkedlist
HashSet<String> othHash = new HashSet<String>(secondArrayList);// ⼩集合⽤hashset
Iterator<String> iter = result.iterator();// 采⽤Iterator迭代器进⾏数据的操作
while(iter.hasNext()){
())){
}
}
resultList = new ArrayList<String>(result);
return resultList;
}
/**
* @⽅法描述:获取两个ArrayList的交集
* @param firstArrayList 第⼀个ArrayList
* @param secondArrayList 第⼆个ArrayList
* @return resultList 交集ArrayList
*/
public static List<String> receiveCollectionList(List<String> firstArrayList, List<String> secondArrayList) {        List<String> resultList = new ArrayList<String>();
LinkedList<String> result = new LinkedList<String>(firstArrayList);// ⼤集合⽤linkedlist
HashSet<String> othHash = new HashSet<String>(secondArrayList);// ⼩集合⽤hashset
Iterator<String> iter = result.iterator();// 采⽤Iterator迭代器进⾏数据的操作
while(iter.hasNext()) {
if(!())) {
}
}
resultList = new ArrayList<String>(result);
return resultList;
}
/**
* @⽅法描述:获取两个ArrayList的去重并集
* @param firstArrayList 第⼀个ArrayList
* @param secondArrayList 第⼆个ArrayList
* @return resultList 去重并集ArrayList
*/
public static List<String> receiveUnionList(List<String> firstArrayList, List<String> secondArrayList) {
List<String> resultList = new ArrayList<String>();
Set<String> firstSet = new TreeSet<String>(firstArrayList);
for(String id : secondArrayList) {
// 当添加不成功的时候说明firstSet中已经存在该对象
firstSet.add(id);
}
resultList = new ArrayList<String>(firstSet);
return resultList;
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。