Here you can find the source of permute(List
public static <E> List<List<E>> permute(List<E> arr)
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Main { public static <E> List<List<E>> permute(List<E> arr) { return permute(arr, 0); }//w w w . j a v a 2 s .c o m public static <E> List<List<E>> permute(List<E> arr, int k) { List<List<E>> permutations = new ArrayList<>(); for (int i = k; i < arr.size(); i++) { Collections.swap(arr, i, k); permutations.addAll(permute(arr, k + 1)); Collections.swap(arr, k, i); } if (k == arr.size() - 1) { permutations.add(new ArrayList<>(arr)); } return permutations; } }