有序佇列比較

即心即佛發表於2018-08-23

場景:
找出兩個有序佇列的相同值與不同值,資料庫中存在佇列A,需要更新為佇列B,佇列A包含且佇列B不包含的刪除,佇列A不包含且佇列B包含的插入,佇列A包含且佇列B包含更新。以下以古代一夫多妻制為例:

public void update(Integer manId,List<Integer> paramList){
        List<Integer> dbList = demoMapper.queryWomanList(manId);
        Collections.sort(dbList);
        Collections.sort(paramList);

        int dbSize = null == dbList ? 0 : dbList.size();
        int paramSize = null == paramList ? 0 : paramList.size();

        int dbIter = 0;
        int paramIter = 0;
        while(dbIter < dbSize || paramIter < paramSize){
            if(dbIter == dbSize){
                demoMapper.insert(manId, paramList.get(paramIter));
                paramIter += 1;
                continue;
            }
            if(paramIter == paramSize){
                demoMapper.delete(manId, dbList.get(dbIter));
                dbIter += 1;
                continue;
            }
            if(dbList.get(dbIter).intValue() == paramList.get(paramIter).intValue()){
                //do update
                dbIter += 1;
                paramIter += 1;
            }else if(dbList.get(dbIter).intValue() > paramList.get(paramIter).intValue()){
                demoMapper.insert(manId, paramList.get(paramIter));
                paramIter += 1;
            }else{
                demoMapper.delete(manId, dbList.get(dbIter));
                dbIter += 1;
            }
        }
    }

完整程式碼請參考:https://github.com/465919283/demo

相關文章