All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.codegas.commons.lang.collection.CollectionUtil Maven / Gradle / Ivy

The newest version!
package org.codegas.commons.lang.collection;

import java.util.*;

public final class CollectionUtil {

    private CollectionUtil() {

    }

    public static  List reverse(Collection collection) {
        List reverse = new ArrayList<>(collection);
        Collections.reverse(reverse);
        return reverse;
    }

    public static  List concat(Collection collection1, Collection collection2) {
        List concat = new ArrayList<>(collection1.size() + collection2.size());
        concat.addAll(collection1);
        concat.addAll(collection2);
        return concat;
    }

    public static > List sort(Collection collection) {
        return sort(collection, Comparable::compareTo);
    }

    public static  List sort(Collection collection, Comparator comparator) {
        List sorted = new ArrayList<>(collection);
        sorted.sort(comparator);
        return sorted;
    }

    public static  List> permute(Collection 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));
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy