(使用了栈)实现 一个map 里面的 集合信息 交替 ,比如: 1234 4321 1234 4321 ......

实现 无线循环 交替 ,比如: 1234  4321 1234 4321 ......


public Integer selectCaseRuleUserTest(int i,String caseRuleName,Map<String,Map<String, LinkedList<Integer>>> map){

    Integer caseRuleUser=null;
    Map<String, LinkedList<Integer>> caseRuleUserMap=map.get(caseRuleName);
    LinkedList<Integer> zhengLinkdList=caseRuleUserMap.get("zheng");
    LinkedList<Integer> fanLinkdList=caseRuleUserMap.get("fan");
    LinkedList<Integer> zhongLinkdList=caseRuleUserMap.get("zhong");
    System.out.println(zhengLinkdList.isEmpty());
    if(!zhengLinkdList.isEmpty()){//判断不为空
        Integer zhengCaseRuleUser=zhengLinkdList.removeFirst();//取出的同时删除
        fanLinkdList.addFirst(zhengCaseRuleUser);
        caseRuleUser=zhengCaseRuleUser;
    }else{//为空
        if(!fanLinkdList.isEmpty()){//不为空
            Integer fanCaseRuleUser=fanLinkdList.removeFirst();//取出的同时删除
            zhongLinkdList.addFirst(fanCaseRuleUser);
            caseRuleUser=fanCaseRuleUser;
        }else if(zhengLinkdList.isEmpty()&&fanLinkdList.isEmpty()){ //先取出一个 在
            Integer zhongCaseRuleUser=zhongLinkdList.removeFirst();
            fanLinkdList.addFirst(zhongCaseRuleUser);
            for (Iterator<Integer> dd = zhongLinkdList.iterator(); dd.hasNext();) {
                Integer caseRuleUser2 = dd.next();
                zhengLinkdList.addLast(caseRuleUser2);
                dd.remove();
            }
            caseRuleUser=zhongCaseRuleUser;
        }
    }
    caseRuleUserMap.put("zheng",zhengLinkdList);
    caseRuleUserMap.put("fan",fanLinkdList);
    caseRuleUserMap.put("zhong",zhongLinkdList);
    map.put(caseRuleName,caseRuleUserMap);
    System.out.println("--------------------end-----------"+caseRuleUser+"--------------------");
    return caseRuleUser;

}

public static void main(String[] args) {

    Map<String,Map<String, LinkedList<Integer>>> map=new HashMap<>();
    Map<String, LinkedList<Integer>> map2=new HashMap<>();

    LinkedList<Integer> zhengList=new LinkedList<Integer>();
    zhengList.add(1);
    zhengList.add(2);
    zhengList.add(3);
    zhengList.add(4);

    LinkedList<Integer> fanList=new LinkedList<>();
    LinkedList<Integer> zhongList=new LinkedList<>();
    map2.put("zheng",zhengList);
    map2.put("fan",fanList);
    map2.put("zhong",zhongList);
    map.put("1",map2);
    CaseRuleServiceImpl caseRuleServiceImpl=new CaseRuleServiceImpl();
    for (int i=0 ;i<50 ; i++ ){
        Integer a=caseRuleServiceImpl.selectCaseRuleUserTest(i,"1",map);
    }

}

另外一种解决办法使用redis 的左进 右取

 

public CaseCollectionGroupUser selectUserByCaseRule(CaseRule caseRule){
    RLock lock = redissonClient.getLock("get_token_lock_" + Group_User_Code+"_"+caseRule.getGroupId());
    CaseCollectionGroupUser user=null;
    try {
        lock.lock();
        if(caseRule!=null){
            if(redis.opsForList().size(Group_User_Code+"_zhe_"+caseRule.getGroupId())<=0){
                List<Object> fanList=redis.opsForList().range(Group_User_Code+"_fan_"+caseRule.getGroupId(),0,-1);
                redis.opsForList().rightPushAll(RedisConstants.Group_User_Code+"_zhe_"+caseRule.getGroupId(),fanList);
                redis.delete(RedisConstants.Group_User_Code+"_fan_"+caseRule.getGroupId());
            }
            //123 123 123 123 左边取 右边塞
            CaseCollectionGroupUser caseCollectionGroupUser=(CaseCollectionGroupUser)redis.opsForList().leftPop(Group_User_Code+"_zhe_"+caseRule.getGroupId());
            redis.opsForList().leftPush(RedisConstants.Group_User_Code+"_fan_"+caseRule.getGroupId(), caseCollectionGroupUser);
            return caseCollectionGroupUser;
        }
    }catch (Exception e){
        e.printStackTrace();
        log.error("redis异常["+e.getMessage()+"]");
    }finally {
        if (lock != null && lock.isHeldByCurrentThread()) {
            lock.unlock();
        }
    }
    return  user;

}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值