有条件排列组合(避开黑名单用户,随机匹配一个用户)

3 篇文章 0 订阅

 避开黑名单用户,随机匹配一个用户

peaceList(既未拉黑,也未被拉黑的用户)

public class UserVO {

    private String userId;
    private String defriendId;

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getDefriendId() {
        return defriendId;
    }

    public void setDefriendId(String defriendId) {
        this.defriendId = defriendId;
    }

}

 

处理过程



            List<String> peaceList = new ArrayList<>();
            peaceList.add("AAA");
//            peaceList.add("FFF");
//            peaceList.add("GGG");
//            peaceList.add("HHH");
//            peaceList.add("KKK");
//            peaceList.add("LLL");

            List<UserVO> userList= new ArrayList<>();
            UserVO addUserVO = new UserVO();
            addUserVO .setUserId("E");
            addUserVO .setDefriendId("B");
            userList.add(addUserVO );
            
            addUserVO = new UserVO();
            addUserVO .setUserId("C");
            addUserVO .setDefriendId("F");
            userList.add(addUserVO );
            
            addUserVO = new UserVO();
            addUserVO .setUserId("C");
            addUserVO .setDefriendId("A");
            userList.add(addUserVO );

            addUserVO = new UserVO();
            addUserVO.setUserId("D");
            addUserVO.setDefriendId("B");
            userList.add(addUserVO);
            
            addUserVO = new UserVO();
            addUserVO.setUserId("A");
            addUserVO.setDefriendId("B");
            userList.add(addUserVO);

            addUserVO = new UserVO();
            addUserVO.setUserId("B");
            addUserVO.setDefriendId("C");
            userList.add(addUserVO);

            addUserVO = new UserVO();
            addUserVO.setUserId("C");
            addUserVO.setDefriendId("D");
            userList.add(addUserVO);

            addUserVO = new UserVO();
            addUserVO.setUserId("D");
            addUserVO.setDefriendId("E");
            userList.add(addUserVO);

            addUserVO = new UserVO();
            addUserVO.setUserId("E");
            addUserVO.setDefriendId("F");
            userList.add(addUserVO);

            addUserVO = new UserVO();
            addUserVO.setUserId("B");
            addUserVO.setDefriendId("F");
            userList.add(addUserVO);

            List<String> allUserList = new ArrayList<>();
            Map<String, List<String>> againstAllMap = new HashMap<>();
            for (int i = 0; i < userList.size(); i++) {
                String userId = userList.get(i).getUserId();
                String value = userList.get(i).getDefriendId();
                allUserList.add(userId);
                allUserList.add(value);
                List<String> againstUserList = new ArrayList<>();
                if (againstAllMap.containsKey(userId)) {
                    againstUserList = againstAllMap.get(userId);
                    againstUserList.add(value);
                    againstAllMap.put(userId, againstUserList);
                } else {
                    againstUserList.add(value);
                    againstAllMap.put(userId, againstUserList);
                }

                againstUserList = new ArrayList<>();
                if (againstAllMap.containsKey(value)) {
                    againstUserList = againstAllMap.get(value);
                    againstUserList.add(userId);
                    againstAllMap.put(value, againstUserList);
                } else {
                    againstUserList.add(userId);
                    againstAllMap.put(value, againstUserList);
                }
            }
            StringUtils.removeSameElement(allUserList);

            System.out.println("**********against**********");
            System.out.println(againstAllMap);

            Map<String, String> resultMap = new HashMap<>();
            Map<String, List<String>> peaceAllMap = new HashMap<>();
            for (int i = 0; i < allUserList.size(); i++) {
                String userId = allUserList.get(i);
                List<String> userPeaceList = new ArrayList<>(allUserList);
                List<String> userUnPeaceList = againstAllMap.get(userId);
                StringUtils.removeSameElement(userUnPeaceList);
                for (int j = 0; j < userUnPeaceList.size(); j++) {
                    userPeaceList.remove(userUnPeaceList.get(j));// 移除所有不相容用户
                }
                if (userPeaceList != null && userPeaceList.size() > 0) {
                    StringUtils.removeSameElement(userPeaceList);
                    userPeaceList.remove(userId);
                    peaceAllMap.put(userId, userPeaceList);
                } else {
                    peaceAllMap.put(userId, new ArrayList<>());
                }
            }

            System.out.println("**********peace**********");
            System.out.println(peaceAllMap);
            System.out.println("**********peace**********");

            List<String> usedUserList = new ArrayList<>();
            Iterator<String> iteratorEnd = peaceAllMap.keySet().iterator();
            while (iteratorEnd.hasNext()) {
                String userId = iteratorEnd.next();
                if (!usedUserList.contains(userId)) {
                    List<String> userPeaceList = peaceAllMap.get(userId);

                    String peaceUserId = "";
                    for (int i = 0; i < userPeaceList.size(); i++) {
                        String userPeaceId = userPeaceList.get(i);
                        if (!usedUserList.contains(userPeaceId)) {
                            peaceUserId = userPeaceId;
                            break;
                        }
                    }
                    if (StringUtils.isEmpty(peaceUserId) && peaceList != null && peaceList.size() > 0) {
                        peaceUserId = peaceList.get(0);
                        peaceList.remove(peaceUserId);
                    }
                    usedUserList.add(userId);
                    usedUserList.add(peaceUserId);
                    resultMap.put(userId, peaceUserId);
                }
            }

            while (peaceList.size() > 1) {
                String userId = peaceList.get(0);
                String peaceUserId = peaceList.get(1);
                resultMap.put(userId, peaceUserId);
                peaceList.remove(userId);
                peaceList.remove(peaceUserId);
            }
            System.out.println(resultMap);
        

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值