• java判断两集合是否相同以及求取交集,并集,差集


    业务中用时需要判断两集合是否相同,所有提供一个工具方法,使用set集合的特性(元素唯一):

    private Map<String,Set<Integer>> getCategoryApiId(Set<Integer> oldAuthSet , Set<Integer> newAuthSet){
            Map<String,Set<Integer>> categoryApiId = new HashMap();
            if (oldAuthSet!=null&&newAuthSet!=null){
                //首先判断两个集合是否一致
                if (oldAuthSet.size()==newAuthSet.size()){
                    Set<Integer> tempSet = new HashSet<Integer>();
                    tempSet.addAll(oldAuthSet);
                    tempSet.addAll(newAuthSet);
                    if (tempSet.size()==oldAuthSet.size()){
                        categoryApiId =null;//未增加api个数
                        logger.info("未新增和删除api");
                    }else{
                        //1:交集;2:a-b的差集;3:并集
                        Set<Integer> apiSet1 = getApiSet(oldAuthSet, newAuthSet, 1);
                        Set<Integer> deleteAuthSet = getApiSet(oldAuthSet, apiSet1, 2);
                        Set<Integer> insertAuthSet = getApiSet(newAuthSet,oldAuthSet , 2);//获取新增的apiId
                        logger.info("删除的api:"+deleteAuthSet);
                        logger.info("新增的api:"+insertAuthSet);
                        categoryApiId.put("insertApi",insertAuthSet);
                        categoryApiId.put("datele",deleteAuthSet);
                    }
                }else{
                    //1:交集;2:a-b的差集;3:b-a的差集;4:并集
                    Set<Integer> apiSet1 = getApiSet(oldAuthSet, newAuthSet, 1);
                    Set<Integer> deleteAuthSet = getApiSet(oldAuthSet, apiSet1, 2);//获取删除的apiId
                    Set<Integer> insertAuthSet = getApiSet(newAuthSet, oldAuthSet, 2);//获取新增的apiId
                    logger.info("删除的api:"+deleteAuthSet);
                    logger.info("新增的api:"+insertAuthSet);
                    categoryApiId.put("insertApi",insertAuthSet);
                    categoryApiId.put("datele",deleteAuthSet);
                }
            }else{
                if (oldAuthSet==null&&newAuthSet!=null){    //全是新增api
                    logger.info("首次新增授权api");
                    categoryApiId.put("insertApi",newAuthSet);
                }else if (oldAuthSet!=null&&newAuthSet==null){  //未修改用户授权api的数据
                    logger.info("未增加新授权的api");
                    categoryApiId.put("datele",oldAuthSet);
                }else if (oldAuthSet==null&&newAuthSet==null){
                    logger.info("已授权api和新增授权api都为空");
                    categoryApiId =null;
                }
            }
            return categoryApiId;
        }
    
    private Set<Integer>  getApiSet(Set<Integer> oldAuthSet, Set<Integer> newAuthSet,int flag) {
            Set<Integer> result = new HashSet<Integer>();
            if(flag==1){ //求交集
                result.clear();
                result.addAll(oldAuthSet);
                result.retainAll(newAuthSet);
            }else if(flag==2){ //求差集
                result.clear();
                result.addAll(oldAuthSet);
                result.removeAll(newAuthSet);
            } else if (flag==3){ //求并集
                result.clear();
                result.addAll(oldAuthSet);
                result.addAll(newAuthSet);
            }
            return result;
        }
  • 相关阅读:
    《设计模式》-原则二:里氏代换原则(LSP)
    设计模式从0开始
    net reactor加密源码保软件安全-net reactor使用教程
    python中import和from...import区别
    Python之import
    c#执行bat批处理文件,并通过线程将结果显示在控件中
    C#中双问号、双冒号等几个特殊关键字
    Eclipse debug高级技巧(转)
    Android从零开始--安装
    以另一个用户来运行程序
  • 原文地址:https://www.cnblogs.com/zyanrong/p/11791508.html
Copyright © 2020-2023  润新知