欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java list 交集 并集 差集 去重复并集

程序员文章站 2022-05-07 10:26:33
...

Array基本操作

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TestCollection {

	public static void main(String[] args) {
		List list1 = new ArrayList();
		list1.add("1111");
		list1.add("2222");
		list1.add("3333");

		List list2 = new ArrayList();
		list2.add("3333");
		list2.add("4444");
		list2.add("5555");

		// 并集
		// list1.addAll(list2);
		// 交集
		// list1.retainAll(list2);
		// 差集
		// list1.removeAll(list2);
		// 无重复并集
		list2.removeAll(list1);
		list1.addAll(list2);

		Iterator<String> it = list1.iterator();
		while (it.hasNext()) {
			System.out.println(it.next());

		}

		// System.out.println("-----------------------------------\n");
		// printStr(list1);

	}

	public static void printStr(List list1) {
		for (int i = 0; i < list1.size(); i++) {
			System.out.println(list1.get(i));
		}
	}
}

 

二、使用commons CollectionUtils工具类

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;

public class CollectionUtilsTest {

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {

        List<String> aList = new ArrayList<String>();
        aList.add("aaa");
        aList.add("bbb");
        aList.add("ccc");
        List<String> bList = new ArrayList<String>();
        bList.add("aaa");
        bList.add("ddd");
        bList.add("eee");
        // 并集
        Collection<String> unionList = CollectionUtils.union(aList, bList);
        // 交集
        Collection<String> intersectionList = CollectionUtils.intersection(aList, bList);
        // 是否存在交集
        boolean isContained = CollectionUtils.containsAny(aList, bList);
        // 交集的补集
        Collection<String> disjunctionList = CollectionUtils.disjunction(aList, bList);
        // 集合相减
        Collection<String> subtractList = CollectionUtils.subtract(aList, bList);
        
        // 排序
        Collections.sort((List<String>) unionList);
        Collections.sort((List<String>) intersectionList);
        Collections.sort((List<String>) disjunctionList);
        Collections.sort((List<String>) subtractList);

        // 测试
        System.out.println("A: " + ArrayUtils.toString(aList.toArray()));
        System.out.println("B: " + ArrayUtils.toString(bList.toArray()));
        System.out.println("A has one of B? : " + isContained);
        System.out.println("Union(A, B): "
                + ArrayUtils.toString(unionList.toArray()));
        System.out.println("Intersection(A, B): "
                + ArrayUtils.toString(intersectionList.toArray()));
        System.out.println("Disjunction(A, B): "
                + ArrayUtils.toString(disjunctionList.toArray()));
        System.out.println("Subtract(A, B): "
                + ArrayUtils.toString(subtractList.toArray()));

    }
}

 

参考:

java list 交集 并集 差集 去重复并集

http://www.blogjava.net/yourfriend/articles/377755.html

 

集合的常用操作类CollectionUtils

http://www.blogjava.net/jakin/archive/2009/05/14/270634.html