org.codegas.commons.lang.collection.CollectionUtil Maven / Gradle / Ivy
package org.codegas.commons.lang.collection;
import java.util.*;
public final class CollectionUtil {
private CollectionUtil() {
}
public static List reverse(Collection extends T> collection) {
List reverse = new ArrayList<>(collection);
Collections.reverse(reverse);
return reverse;
}
public static List concat(Collection extends T> collection1, Collection extends T> collection2) {
List concat = new ArrayList<>(collection1.size() + collection2.size());
concat.addAll(collection1);
concat.addAll(collection2);
return concat;
}
public static > List sort(Collection extends T> collection) {
return sort(collection, Comparable::compareTo);
}
public static List sort(Collection extends T> collection, Comparator super T> comparator) {
List sorted = new ArrayList<>(collection);
sorted.sort(comparator);
return sorted;
}
public static List> permute(Collection extends T> list) {
List> permutations = new ArrayList<>();
permute(new ArrayList<>(list), 0, permutations);
return permutations.isEmpty() ? Collections.singletonList(new ArrayList<>()) : permutations;
}
private static void permute(List list, int permutationIndex, Collection> permutations) {
for (int index = permutationIndex; index < list.size(); index++) {
Collections.swap(list, index, permutationIndex);
permute(list, permutationIndex + 1, permutations);
Collections.swap(list, permutationIndex, index);
}
if (permutationIndex == list.size() - 1) {
permutations.add(new ArrayList<>(list));
}
}
}