Java List Permutate permutations(List listings)

Here you can find the source of permutations(List listings)

Description

permutations

License

Open Source License

Declaration

public static <T> List<List<T>> permutations(List<T> listings) throws Exception 

Method Source Code


//package com.java2s;
//License from project: MIT License 

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static <T> List<List<T>> permutations(List<T> listings) throws Exception {
        /*/*from   ww w.j av  a2  s. com*/
         * String debug = "["; for (int i = 0; i < listings.size(); ++i) { debug
         * += listings.get(i).toString(); if (i < (listings.size() - 1)) { debug
         * += ", "; } } debug += "]"; System.out.println("permutations(" + debug
         * + ")");
         */

        List<List<T>> perms = new ArrayList<List<T>>();

        if (listings.size() == 0) {
            // System.out.println("permutations return list of size 0");
            return perms;
        }

        List<T> list = new ArrayList<T>();
        list.add(listings.get(0));
        perms.add(list);

        if (listings.size() == 1) {
            // System.out.println("permutations return list of size " +
            // perms.size());
            return perms;
        }

        List<List<T>> perms2 = permutations(listings.subList(1, listings.size()));
        perms.addAll(perms2);

        for (List<T> l : perms2) {
            List<T> newList = new ArrayList<T>(l);
            newList.add(listings.get(0));
            perms.add(newList);
        }

        // System.out.println("permutations return list of size " +
        // perms.size());
        return perms;
    }
}

Related

  1. permsList(List topList)
  2. permutate(List src)
  3. permutation(List list1, List list2, String token)
  4. permutation(String prefix, String s, List list)
  5. permutations(List list)
  6. permutations(Map> parameterValues)
  7. permute(List s1, List s2)
  8. permute(List arr)
  9. permute(List list1)