集合元素排差

集合元素排差:

1.从list集合找出不存在于array集合元素
2.从list1集合找出不存在于list2集合元素
3.从list1集合找出存在于list2集合元素
4.结果集元素去重

一、list不存在array集合元素

范例:批量录入人员工号相关信息,系统中不存在工号问题
/**
     * list集合不存在array元素
     */
    public void listComparisonArray(){
        //模拟创建对比集合
        String[] array = new String[20];
        List<String> list = new ArrayList();
        //循环第一个集合取出元素
        String one = null;
        //循环第二个集合取出元素
        String two = null;
        for(int a = 1;a <= array.length ;a++){
            //array插入元素
            array[a-1] = ""+a;
            if(a % 2 == 0){
                //list集合动态插入x个数
                list.add(""+(a+1));
                if((a+1)>array.length){
                    list.add(""+(a+1));
                    list.add(""+(a+3));
                }
            }
        }
        //两个集合双层嵌套循环
        for (int i = 0; i < list.size();i++) {
            //List集合取数
            one = list.get(i);
            for (int j = 0;j < array.length;j++){
                //array集合取数
                two = array[j];
                //List集合内存在array集合的数
                if(one.equals(two)){
                    //通过移出集合差数
                    list.remove(i);
                    //避免移除后下次取数跳过一位对比
                    i--;
                }
            }
        }
        //结果集去重
        List rList= removeRepeat(list);
        System.out.println("不存在元素:"+String.join(",", rList));
    }

二、list不存在list集合元素

范例:批量录入人员工号相关信息,系统中不存在工号问题
/**
     *  list集合不存在list元素
     */
    public void listComparisonList(){
        //测试数据
        Map map1=new HashMap();
        map1.put("姓名","洪涛");
        Map map2=new HashMap();
        map2.put("姓名","鸿腾");
        Map map3=new HashMap();
        map3.put("姓名","宏图");
        Map map4=new HashMap();
        map4.put("姓名","宏伟");
        Map map5=new HashMap();
        map5.put("姓名","洪涛");
        Map map6=new HashMap();
        map6.put("姓名","鸿涛");
        Map map7=new HashMap();
        map7.put("姓名","鸿涛");
        //模拟创建集合一
        List<Map> list1 = new ArrayList<>();
        list1.add(map1);
        list1.add(map2);
        list1.add(map3);
        list1.add(map4);
        //模拟创建集合二
        List<Map> list2 = new ArrayList<>();
        list2.add(map5);
        list2.add(map6);
        list2.add(map7);


        //循环第一个集合取出元素
        String one = null;
        //循环第二个集合取出元素
        String two = null;
        for (int x = 0;x < list2.size();x++) {
            one = (String) list2.get(x).get("姓名");
            for (int y = 0;y < list1.size();y++){
                two = (String) list1.get(y).get("姓名");
                //对比相同的移除
                if (one.equals(two)){
                    list2.remove(list2.get(x));
                    x--;
                }
            }
        }
        //结果集去重
        List rList= removeRepeat(list2);
        System.out.println("不存在元素:"+rList);
    }

三、list存在list集合元素

范例:批量录入身份证号,系统中证件号已存在问题
/**
     *  list集合不存在list元素
     */
    public void listComparisonLists(){
        //测试数据
        Map map1=new HashMap();
        map1.put("姓名","洪涛");
        Map map2=new HashMap();
        map2.put("姓名","鸿腾");
        Map map3=new HashMap();
        map3.put("姓名","宏图");
        Map map4=new HashMap();
        map4.put("姓名","宏伟");
        Map map5=new HashMap();
        map5.put("姓名","洪涛");
        Map map6=new HashMap();
        map6.put("姓名","鸿涛");
        Map map7=new HashMap();
        map7.put("姓名","洪涛");
        //模拟创建集合一
        List<Map> list1 = new ArrayList<>();
        list1.add(map1);
        list1.add(map2);
        list1.add(map3);
        list1.add(map4);
        //模拟创建集合二
        List<Map> list2 = new ArrayList<>();
        list2.add(map5);
        list2.add(map6);
        list2.add(map7);
        //存储两个集合都存在元素
        List result = new ArrayList();
        //循环第一个集合取出元素
        String one = null;
        //循环第二个集合取出元素
        String two = null;
        for (Map<String,String> Mapa : list2) {
            one = Mapa.get("姓名");
            for (Map<String,String> Mapb : list1){
                two = Mapb.get("姓名");
                //对比相同的添加
                if (one.equals(two)){
                    result.add(one);
                }
            }
        }
        //结果集去重
        List rList= removeRepeat(result);
        System.out.println("存在元素:"+rList);
    }

四、公共方法处理存储同一元素多次不存在/存在数据

/**
     * 集合内去重
     * @param list
     * @return doubleList
     */
    public static List<String> removeRepeat(List list){
        List doubleList= new ArrayList();
        Set set = new HashSet();
        set.addAll(list);
        doubleList.addAll(set);
        return doubleList;
    }

五、主方法

import java.io.IOException;
import java.util.*;

public class Test {
  Test test = new Test();
        test.listComparisonArray();
        test.listComparisonList();
        test.listComparisonLists();
}

六、执行结果

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容