Java gets the difference of the collection, de-duplication union, and intersection (suitable for data more than 100,000)

The collection operation directly uses remove to compare the difference between the two collections. When the data volume reaches more than 100,000, the efficiency is relatively slow.It is recommended to use the following method.

package com.lcw.adapter;

import java.util.*;

/**
 * @author: craywen
 * @date: 2021-02-22 16:07
 * @desc:
 */
public class set {
    
    
    public static void main(String args[]) {
    
    
        getList();
    }

    // 获取两个ArrayList的差集、交集、去重并集(数据量大小不限制)
    private static void getList() {
    
    
        Collection<String> firstArrayList = new ArrayList<String>();
        Collection<String> secondArrayList = new ArrayList<String>();
        Collection<String> defectList = new ArrayList<String>();//差集List
        Collection<String> collectionList = new ArrayList<String>();//交集List
        Collection<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.println(defectIterator.next());
            }
            // 获取交集
            collectionList = receiveCollectionList(firstArrayList, secondArrayList);
            Iterator<String> collectionIterator = collectionList.iterator();
            System.out.println("===================交集===================");
            while(collectionIterator.hasNext()) {
    
    
                System.out.println(collectionIterator.next());
            }
            // 获取去重并集
            unionList = receiveUnionList(firstArrayList, secondArrayList);
            Iterator<String> unionIterator = unionList.iterator();
            System.out.println("===================去重并集===================");
            while(unionIterator.hasNext()) {
    
    
                System.out.println(unionIterator.next());
            }
        }catch(Exception e) {
    
    
            e.printStackTrace();
        }
    }

    /**
     * @方法描述:获取两个ArrayList的差集
     * @param firstArrayList 第一个Collection
     * @param secondArrayList 第二个Collection
     * @return resultList 差集Collection
     */
    public static Collection<String> receiveDefectList(Collection<String> firstArrayList, Collection<String> secondArrayList) {
    
    
        Collection<String> resultList = new ArrayList<String>();
        LinkedList<String> result = new LinkedList<String>(firstArrayList);// 大集合用linkedlist
        HashSet<String> othHash = new HashSet<String>(secondArrayList);// 小集合用hashset
        // 采用Iterator迭代器进行数据的操作
        result.removeIf(othHash::contains);
        resultList = new ArrayList<String>(result);
        return resultList;
    }

    /**
     * @方法描述:获取两个ArrayList的交集
     * @param firstArrayList 第一个Collection
     * @param secondArrayList 第二个Collection
     * @return resultList 交集Collection
     */
    public static Collection<String> receiveCollectionList(Collection<String> firstArrayList, Collection<String> secondArrayList) {
    
    
        Collection<String> resultList = new ArrayList<String>();
        LinkedList<String> result = new LinkedList<String>(firstArrayList);// 大集合用linkedlist
        HashSet<String> othHash = new HashSet<String>(secondArrayList);// 小集合用hashset
        // 采用Iterator迭代器进行数据的操作
        result.removeIf(s -> !othHash.contains(s));
        resultList = new ArrayList<String>(result);
        return resultList;
    }

    /**
     * @方法描述:获取两个ArrayList的去重并集
     * @param firstArrayList 第一个Collection
     * @param secondArrayList 第二个Collection
     * @return resultList 去重并集Collection
     */
    public static Collection<String> receiveUnionList(Collection<String> firstArrayList, Collection<String> secondArrayList) {
    
    
        Collection<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;
    }

}

Reposted: https://www.cnblogs.com/o0shine0o-zp/p/6902392.html

Guess you like

Origin blog.csdn.net/qq_38893133/article/details/113942130