list All Combinations - Java Collection Framework

Java examples for Collection Framework:List

Description

list All Combinations

Demo Code


//package com.java2s;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Main {
    public static void main(String[] argv) throws Exception {
        int n = 2;
        boolean zeroBased = true;
        System.out.println(listAllCombinations(n, zeroBased));
    }/* www.ja  v  a  2s.c  om*/

    public static List<Set<Integer>> listAllCombinations(int n,
            boolean zeroBased) {
        List<Set<Integer>> allCombinations;

        allCombinations = new ArrayList<Set<Integer>>();
        for (int k = 0; k < n; k++) {
            allCombinations.addAll(listCombinationsFixed2(n, k, zeroBased));
        }

        return allCombinations;
    }

    public static List<Set<Integer>> listCombinationsFixed2(final int n,
            final int k, final boolean isZeroBased) {
        List<Set<Integer>> combinations = new ArrayList<Set<Integer>>();

        //System.out.println("will get combinations with (n,k)=(" + n + "," + k + ")");

        Integer[] combination = null;

        if (k == 0 || k == n) {
            combination = new Integer[n];
            for (int i = 0; i < n; i++) {
                combination[i] = i;
            }
        } else {
            combination = new Integer[k];
            for (int i = 0; i < k; i++) {
                combination[i] = i;
            }
        }

        if (isZeroBased) {
            combinations.add(new HashSet<Integer>(Arrays
                    .asList(combination)));
        } else {
            Integer[] tempCombination = new Integer[combination.length];

            for (int i = 0; i < combination.length; i++) {
                tempCombination[i] = combination[i] + 1;
            }
            combinations.add(new HashSet<Integer>(Arrays
                    .asList(tempCombination)));
        }

        while (nextCombination(combination, n, k)) {

            if (isZeroBased) {
                combinations.add(new HashSet<Integer>(Arrays
                        .asList(combination)));
            } else {
                Integer[] tempCombination = new Integer[combination.length];

                for (int i = 0; i < combination.length; i++) {
                    tempCombination[i] = combination[i] + 1;
                }
                combinations.add(new HashSet<Integer>(Arrays
                        .asList(tempCombination)));
            }

        }

        return combinations;
    }

    private static boolean nextCombination(Integer comb[], int n, int k) {
        int i = k - 1;
        if (i == -1) {
            return false;
            //System.out.println("NOT VALID INDEX, n:" + n + ", k:" + k);
        }

        ++comb[i];
        while ((i >= 0) && (comb[i] >= n - k + 1 + i)) {
            --i;
            if (i >= 0) {
                ++comb[i];
            }
        }

        if (comb[0] > n - k) /* Combination (n-k, n-k+1, ..., n) reached */
            return false; /* No more combinations can be generated */

        /* comb now looks like (..., x, n, n, n, ..., n).
        Turn it into (..., x, x + 1, x + 2, ...) */
        for (i = i + 1; i < k; ++i)
            comb[i] = comb[i - 1] + 1;

        return true;
    }
}

Related Tutorials