Java Collection Combine combine(final Collection[] c)

Here you can find the source of combine(final Collection[] c)

Description

Functions that makes every possible combination of items from an array of collection while respecting the order of the collections.

License

Open Source License

Declaration

static public <V> Collection<List<V>> combine(final Collection<V>[] c) 

Method Source Code


//package com.java2s;
/* Utils.java/*from   ww  w . ja v a  2  s  . c o  m*/
 * Copyright (C) 2010 Gr?goire D?trez, Ramona Enache
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

import java.util.*;

public class Main {
    /** Functions that makes every possible combination of items from
     * an array of collection while respecting the order of the collections.
     */
    static public <V> Collection<List<V>> combine(final Collection<V>[] c) {
        Collection<List<V>> result = new Vector();
        Vector<V> combination = new Vector(c.length);

        /* We initialise the state with iterators from all collections 
         * and the combination with the initial value from all iterators */
        Iterator<V>[] state = new Iterator[c.length];
        for (int i = 0; i < c.length; i++) {
            state[i] = c[i].iterator();
            if (state[i].hasNext())
                combination.add(state[i].next());
            else
                return result;
        }
        result.add((List<V>) combination.clone());

        int pos = c.length - 1;
        while (pos >= 0) {
            if (state[pos].hasNext()) {
                combination.set(pos, state[pos].next());
                if (pos < c.length - 1)
                    pos++;
            } else {
                state[pos] = c[pos].iterator();
                pos--;
            }
            result.add((List<V>) combination.clone());
        }
        return result;
    }
}

Related

  1. combine(Collection first, Collection second)
  2. combine(Collection... collections)
  3. combine(Collection strings)
  4. combine(String separator, Collection parts)
  5. combine(String separator, Collection stringCollection)
  6. combine(String separator, Collection objs)
  7. combineAndRemoveDuplicates(Collection collection1, Collection collection2)