条件付き配列の組合せ(ブラックリストユーザーを避け、ランダムに1人のユーザーに一致する)


ブラックリストのユーザーを避け、ランダムに1人のユーザーに一致させる
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 peaceList = new ArrayList<>();
            peaceList.add("AAA");
//            peaceList.add("FFF");
//            peaceList.add("GGG");
//            peaceList.add("HHH");
//            peaceList.add("KKK");
//            peaceList.add("LLL");

            List 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 allUserList = new ArrayList<>();
            Map> 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 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 resultMap = new HashMap<>();
            Map> peaceAllMap = new HashMap<>();
            for (int i = 0; i < allUserList.size(); i++) {
                String userId = allUserList.get(i);
                List userPeaceList = new ArrayList<>(allUserList);
                List 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 usedUserList = new ArrayList<>();
            Iterator iteratorEnd = peaceAllMap.keySet().iterator();
            while (iteratorEnd.hasNext()) {
                String userId = iteratorEnd.next();
                if (!usedUserList.contains(userId)) {
                    List 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);