Here you can find the source of permute(List
public static <E> List<List<E>> permute(List<E> list1)
//package com.java2s; /*/*w ww .j av a2 s. co m*/ Copyright 2009 Semantic Discovery, Inc. (www.semanticdiscovery.com) This file is part of the Semantic Discovery Toolkit. The Semantic Discovery Toolkit 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 3 of the License, or (at your option) any later version. The Semantic Discovery Toolkit 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 The Semantic Discovery Toolkit. If not, see <http://www.gnu.org/licenses/>. */ import java.util.List; import java.util.ArrayList; import java.util.LinkedList; import java.util.Iterator; public class Main { /** * Create a list of all permutations (lists) of the elements within * the given list. */ public static <E> List<List<E>> permute(List<E> list1) { List<List<E>> result = new ArrayList<List<E>>(); if (list1 != null) { int s1 = list1.size(); if (s1 > 0) { int index = 0; for (Iterator<E> it = list1.iterator(); it.hasNext(); ++index) { E elt = it.next(); List<E> list2 = new LinkedList<E>(list1.subList(0, index)); list2.addAll(list1.subList(index + 1, s1)); List<List<E>> p2 = permute(list2); for (List<E> curList : p2) { ((LinkedList<E>) curList).addFirst(elt); result.add(curList); } } } else { result.add(new LinkedList<E>()); } } return result; } }