迭代和组合存储在三个arraylist中的元素

时间:2015-08-05 06:58:27

标签: java arraylist combinations permutation

HashMap<String, Set<ArrayList<String>>> alMealtypeComb = new HashMap<>();
                    for (int nextCount = 0; nextCount < roomListHM.get("Room"+checkIndex).size(); nextCount++) {
                        if(checkIndex<roomIndexCounter-1)
                        {
                            checkIndex++;
                        }
                        for (int outter = 1; outter < roomListHM.get("Room" + roomCount).get(maxIndex).size(); outter++) {
                            alcombination = new ArrayList<>();

                            permutations = new ArrayList<>();
                            if (roomIndexCounter > 1) {
                                roomDataCounter = 1;
                            }

                            int data;

                            start: for (int roomComb = 0; roomComb < valueSize; roomComb++) {
                                if (roomIndexCounter > roomComb) {
                                    chflag = false;
                                    if (!chflag && roomIndexCounter != 1) {
                                        if ((roomListHM.get("Room" + roomComb).get(roomComb)).contains(roomListHM.get("Room" + roomCount)
                                                .get(maxIndex).get(outter))) {
                                            keyMealType = roomListHM.get("Room" + roomCount).get(maxIndex).get(outter);
                                            alcombination.add(roomListHM.get("Room" + roomComb).get(nextCount).get(0) + "--" + roomComb + "--"
                                                    + keyMealType);
                                            chflag = true;
                                        }
                                    }
                                    chflag = false;
                                    if (!chflag && roomIndexCounter != 1) {
                                        if ((roomListHM.get("Room" + roomComb).get(roomComb)).contains(roomListHM.get("Room" + roomCount)
                                                .get(maxIndex).get(outter))) {
                                            keyMealType = roomListHM.get("Room" + roomCount).get(maxIndex).get(outter);
                                            alcombination.add(roomListHM.get("Room" + roomComb).get(roomComb).get(0) + "--" + roomComb + "--"
                                                    + keyMealType);
                                            chflag = true;
                                        }
                                    }
                                //}

                                    if ((roomListHM.get("Room" + roomDataCounter).get(roomComb)).contains(roomListHM.get("Room" + roomCount)
                                            .get(maxIndex).get(outter))) {
                                        keyMealType = roomListHM.get("Room" + roomCount).get(maxIndex).get(outter);
                                        alcombination.add(roomListHM.get("Room" + roomDataCounter).get(roomComb).get(0) + "--" + roomDataCounter
                                                + "--" + keyMealType);

                                    }

                                    if (roomDataCounter < roomIndexCounter && roomComb == valueSize - 1 && roomIndexCounter != 1) {

                                        roomDataCounter++;
                                        continue start;
                                    }
                                }

                            }

                            roomCombination(new ArrayList<String>(), alcombination);
                            for (int j = 0; j < permutations.size(); j++) {
                                if (permutations.get(j).size() != roomIndexCounter) {
                                    permutations.remove(j);

                                    j--;

                                }
                            }
                            if (roomIndexCounter > 1) {

                                for (int j = 0; j < permutations.size(); j++) {
                                    int check = 0;

                                    if (roomIndexCounter == 2) {
                                        if ((permutations.get(j).toString()).contains("" + 0 + "")
                                                && (permutations.get(j).toString()).contains("" + 1 + "")) {
                                            continue;

                                        } else {
                                            permutations.remove(j);
                                            j--;

                                        }
                                    }
                                    if (roomIndexCounter == 3) {
                                        if ((permutations.get(j).toString()).contains("" + 0 + "")
                                                && (permutations.get(j).toString()).contains("" + 1 + "")
                                                && (permutations.get(j).toString()).contains("" + 2 + "")) {
                                            continue;

                                        } else {
                                            permutations.remove(j);
                                            j--;

                                        }
                                    }


                                }
                            }

                             Set<ArrayList<String>> newSet=new
                             HashSet<>(permutations);
                             System.out.println("set value*********"+newSet);

                            //alMealtypeComb.put(keyMealType, permutations);
                            alMealtypeComb.put(keyMealType, newSet);

                            System.out.println("result" + permutations);

                        }
                    }
                    hmRoomCombionation.put("Room" + roomCount, alMealtypeComb);

                }

                System.out.println("**********************" + hmRoomCombionation);

            }

        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return alHotelResult;
}

// combination with recursion

public static void roomCombination(ArrayList<String> sub, ArrayList<String> letterArray) {
    permutations.add(sub);
    if (letterArray.size() != 0) {
        for (int i = 0; i < letterArray.size(); i++) {
            ArrayList<String> prefix = new ArrayList<String>(sub);
            prefix.add(letterArray.get(i));
            ArrayList<String> postfix = new ArrayList<String>(letterArray);
            postfix.remove(i);
            roomCombination(prefix, postfix);
        }
    }
}

我需要迭代并形成存储在指定大小的ArrayList中的元素组合。

例如:

假设我们有三个arraylist如下:

Room1[DLX1-HB,DLX1-FB,DLX1-RO,CLB1-HB,CLB1-FB,CLB1-RO]

Room2[DLX2-HB,DLX2-FB,DLX2-RO,DLX2-BF,CLB2-HB,CLB2-FB,CLB2-RO] 

Room3[DLX3-HB,DLX3-FB,DLX3-RO,CLB3-HB,CLB3-FB,CLB3-RO,CLB3-BF]

在这种情况下,我需要3的组合。

示例输出:

[DLX1-HB,DLX2-HB,DLX3-HB]

[DLX1-HB,CLB2-HB,DLX3-HB]

[DLX1-FB,DLX2-FB,DLX3-FB]

.

.

然后是模式。

请以最小的循环为我提供优化的解决方案。

0 个答案:

没有答案