判断两个数组是否存在交集

简介 使用场景:代码:@Slf4jpublic class UserSetGroupRangMatcheUtil {    /**     * 判断两个数组是否存在交集     * a最大值< b最大

🔔🔔好消息!好消息!🔔🔔

 如果您需要注册ChatGPT,想要升级ChatGPT4。凯哥可以代注册ChatGPT账号代升级ChatGPT4

有需要的朋友👉:微信号 kaigejava2022

使用场景:



代码:

@Slf4j
public class DateRangMatcheUtil {


    /**
     * 判断两个数组是否存在交集
     * a最大值< b最大值 true
     * a最大值 > b最大值
     * a的最小值< b最大值 false
     * a的最小值 > b的最大值 true
     * <p>
     * a的最大值=b的最大值 false
     * a的最大值=b的最小值false
     * a的最小值=b的最大值 false
     * a的最小值=b的最小值 false
     *
     * @param param           param
     * @param comparableParam db
     * @return
     */
    public static boolean isIntervalOverlap(Integer[] param, Integer[] comparableParam) {
        if (null == param || comparableParam == null) {
            throw new RuntimeException("区间范围必须包含至少1个值以上");
        }
        Arrays.sort(param);
        Arrays.sort(comparableParam);
        int aStart = param[0];
        int aEnd = param[param.length - 1];
        int bStart = comparableParam[0];
        int bEnd = comparableParam[param.length - 1];

        if (aStart == bStart) {
            return true;
        }
        if (aStart == bEnd) {
            return true;
        }


        if (aEnd == bStart) {
            return true;
        }
        if (aEnd == bEnd) {
            return true;
        }
        // Integer[] param = new Integer [] {1,4};
        // Integer[] comparableParam = new Integer [] {3,5};

        // a:5,6
        //b:8 ,9
        // a: 4,4
        //b :3,5
        if (aEnd < bEnd) {
            if (aStart >= bStart || (aEnd > bStart && aEnd < bEnd)) {
                return true;
            }
            return false;
        }

        if (aEnd > bEnd) {
            //比较的comArr:[1, 4],dbRange:[0, 3]
            // a:[1,5] b:[0,3]  true
            // a:[4,4] b:[0,3]  false
            // a:[4,9] b:[0,3]  false
            if (aStart > bStart && aStart < bEnd) {
                return true;
            }
            return false;





           /* if(aStart < bEnd){
                return false;
            }*/

        }


        return false;
    }


}

使用:

 //获取当前db范围
            Map<Long,UserSetGroupVo> dbMap = dbList.stream().collect(Collectors.toMap(UserSetGroupVo::getId, a -> a, (k1, k2) -> k1));
            //循环比较
            for(UserSetGroupVo vo: userSetGroupVoList){
                Long id = vo.getId();
                if(null != id){
                    UserSetGroupVo mapVo = dbMap.get(id);
                    if(null != mapVo){
                        if(!vo.getMinUserId().equals(mapVo.getMinUserId()) || !vo.getMaxUserId().equals(mapVo.getMaxUserId())){
                            log.info("最大及最小值发生了变化,比较新值是否已经存在");
                            Integer[] comArr = new Integer[]{vo.getMinUserId(),vo.getMaxUserId()};
                            dbArrList.forEach(dbRange->{
                                boolean result = UserSetGroupRangUtil.isIntervalOverlap(comArr,dbRange);
                                if(result){
                                    String str ="当前数据库中范围:["+dbRange[0]+","+dbRange[1]+"]。当前范围:["+vo.getMinUserId()+","+vo.getMaxUserId()+"]";
                                    throw new DZException(ErrorCode.SAVE_USER_SET_GROUP_COMP_FAILED.getCode(),"范围已经存在。"+str);
                                }
                            });
                        }
                    }
                }else {
                    //新增的。判断是否存在交集
                    Integer[] comArr = new Integer[]{vo.getMinUserId(),vo.getMaxUserId()};
                    for(Integer [] dbRange :dbArrList){
                        log.info("比较的comArr:{},dbRange:{}",Arrays.asList(comArr),Arrays.asList(dbRange));
                        boolean result = UserSetGroupRangUtil.isIntervalOverlap(comArr,dbRange);
                        if(result){
                            String str ="当前数据库中范围:["+dbRange[0]+","+dbRange[1]+"]。当前范围:["+vo.getMinUserId()+","+vo.getMaxUserId()+"]";
                            throw new DZException(ErrorCode.SAVE_USER_SET_GROUP_COMP_FAILED.getCode(),"范围已经存在。"+str);
                        }
                    }
                }
            }


TopTop