Java List Permutate permsList(List topList)

Here you can find the source of permsList(List topList)

Description

Given a mainlist of sublists, return a new newlist of newsublists, such that: - the number of newsublists is the number of "cycles" ("permutations"?) - each newsublist has the same number of objects in it as the original mainlist had sublists.

License

Open Source License

Declaration

public static List permsList(List topList) 

Method Source Code

//package com.java2s;
/**//from w w  w. j a  v  a 2 s . c o m
 * Util.java
 * @author John Green
 * 27-Oct-2002
 * www.joanju.com
 * 
 * Copyright (c) 2002 Joanju Limited.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 */

import java.util.*;

public class Main {
    /** Given a mainlist of sublists, return a new newlist of
     * newsublists, such that:
     *   - the number of newsublists is the number of "cycles"
     *     ("permutations"?)
     *   - each newsublist has the same number of objects in it
     *     as the original mainlist had sublists. There is one
     *     object from each of the original sublists.
     * (i.e. If the mainlist contains 3 sublists, and the number
     * of items in the sublists is: 2, 1, 4, then the number of 
     * cycles is: 2 * 1 * 4 = 8. The newlist will contain 8 newsublists
     * with three objects each.)
     * (Might be easier to think of the return list as a matrix of m
     * by n, where m (rows) is the number of original sublists, and n
     * (columns) is the number of cycles/permutations.)
     * I'm sure there's proper terminology and algorithms for this, but
     * I don't know what they are.
     */
    public static List permsList(List topList) {
        int numSublists = topList.size();
        if (numSublists == 0)
            return topList;
        List retList = new ArrayList();
        List firstElements = (List) topList.get(0);
        List tailPerms = permsList(topList.subList(1, numSublists));
        for (Iterator it = firstElements.iterator(); it.hasNext();) {
            Object item = it.next();
            if (tailPerms.size() > 0) {
                for (Iterator it2 = tailPerms.iterator(); it2.hasNext();) {
                    List subPermList = (List) it2.next();
                    List addList = new ArrayList();
                    addList.add(item);
                    addList.addAll(subPermList);
                    retList.add(addList);
                }
            } else {
                List addList = new ArrayList();
                addList.add(item);
                retList.add(addList);
            }
        }
        return retList;
    }
}

Related

  1. perm(List l)
  2. permutate(List src)
  3. permutation(List list1, List list2, String token)
  4. permutation(String prefix, String s, List list)
  5. permutations(List list)