Tuesday 20 January 2015

Get Combinations


Combination is a collection of things, where order doesn't matter.

Example
import org.paukov.combinatorics.Factory;
import org.paukov.combinatorics.Generator;
import org.paukov.combinatorics.ICombinatoricsVector;

public class CombinationEx {

    public static void main(String args[]) {
        // Create the initial vector
        ICombinatoricsVector<Character> initialVector = Factory.createVector(
                new Character[]{'a', 'b', 'c', 'd', 'e'});

        // Create a simple combination generator to generate 3-combinations of the initial vector
        Generator<Character> gen = Factory.createSimpleCombinationGenerator(initialVector, 3);

        // Print all possible combinations
        for (ICombinatoricsVector<Character> combination : gen) {
            System.out.println(combination);
        }
    }
}


Output
CombinatoricsVector=([a, b, c], size=3)
CombinatoricsVector=([a, b, d], size=3)
CombinatoricsVector=([a, b, e], size=3)
CombinatoricsVector=([a, c, d], size=3)
CombinatoricsVector=([a, c, e], size=3)
CombinatoricsVector=([a, d, e], size=3)
CombinatoricsVector=([b, c, d], size=3)
CombinatoricsVector=([b, c, e], size=3)
CombinatoricsVector=([b, d, e], size=3)
CombinatoricsVector=([c, d, e], size=3)


Combinations with repetitions
import org.paukov.combinatorics.Factory;
import org.paukov.combinatorics.Generator;
import org.paukov.combinatorics.ICombinatoricsVector;

public class CombinationEx {

    public static void main(String args[]) {
        // Create the initial vector
        ICombinatoricsVector<Character> initialVector = Factory.createVector(
                new Character[]{'a', 'b', 'c', 'd', 'e'});

        // Create a multi combination generator to generate 3-combinations of the initial vector
        Generator<Character> gen = Factory.createMultiCombinationGenerator(initialVector, 3);

        System.out.println("Total number of objects generated " + gen.getNumberOfGeneratedObjects());

        // Print all possible combinations
        for (ICombinatoricsVector<Character> combination : gen) {
            System.out.println(combination);
        }
    }
}


Output
Total number of objects generated 35
CombinatoricsVector=([a, a, a], size=3)
CombinatoricsVector=([a, a, b], size=3)
CombinatoricsVector=([a, a, c], size=3)
CombinatoricsVector=([a, a, d], size=3)
CombinatoricsVector=([a, a, e], size=3)
CombinatoricsVector=([a, b, b], size=3)
CombinatoricsVector=([a, b, c], size=3)
CombinatoricsVector=([a, b, d], size=3)
CombinatoricsVector=([a, b, e], size=3)
CombinatoricsVector=([a, c, c], size=3)
CombinatoricsVector=([a, c, d], size=3)
CombinatoricsVector=([a, c, e], size=3)
CombinatoricsVector=([a, d, d], size=3)
CombinatoricsVector=([a, d, e], size=3)
CombinatoricsVector=([a, e, e], size=3)
CombinatoricsVector=([b, b, b], size=3)
CombinatoricsVector=([b, b, c], size=3)
CombinatoricsVector=([b, b, d], size=3)
CombinatoricsVector=([b, b, e], size=3)
CombinatoricsVector=([b, c, c], size=3)
CombinatoricsVector=([b, c, d], size=3)
CombinatoricsVector=([b, c, e], size=3)
CombinatoricsVector=([b, d, d], size=3)
CombinatoricsVector=([b, d, e], size=3)
CombinatoricsVector=([b, e, e], size=3)
CombinatoricsVector=([c, c, c], size=3)
CombinatoricsVector=([c, c, d], size=3)
CombinatoricsVector=([c, c, e], size=3)
CombinatoricsVector=([c, d, d], size=3)
CombinatoricsVector=([c, d, e], size=3)
CombinatoricsVector=([c, e, e], size=3)
CombinatoricsVector=([d, d, d], size=3)
CombinatoricsVector=([d, d, e], size=3)
CombinatoricsVector=([d, e, e], size=3)
CombinatoricsVector=([e, e, e], size=3)

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment