aggregate array of Collections - Java java.util

Java examples for java.util:Collection Operation

Description

aggregate array of Collections

Demo Code


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Main{
    public static void main(String[] argv){
        Collection collections = java.util.Arrays.asList("asdf","java2s.com");
        System.out.println(aggregate(collections));
    }/*w  ww  .j  a  v  a2 s .c o m*/
    public static <T> Collection<T> aggregate(Collection<T>... collections) {
        Collection<T> newAggregation = newCollection((Collection<T>) null);
        for (Collection<T> collection : collections) {
            newAggregation.addAll(collection);
        }

        return newAggregation;
    }
    public static <T> Collection<T> aggregate(Collection<T> collection,
            T... elements) {
        Collection<T> newCollection = newCollection(collection);
        for (T t : elements) {
            newCollection.add(t);
        }

        return newCollection;
    }
    public static <T> List<T> asList(Collection<T> collection) {
        if (isNotEmpty(collection))
            return new ArrayList<T>(collection);

        return new ArrayList<T>();
    }
    public static <T> List<T> asList(T[] elements) {
        return newList(elements);
    }
    public static <T> Collection<T> newCollection(Collection<T> collection) {
        if (isNotEmpty(collection)) {
            ArrayList<T> arrayList = new ArrayList<T>(collection.size());
            arrayList.addAll(collection);
            return arrayList;
        }

        return new ArrayList<T>();
    }
    public static <T> Collection<T> newCollection(T... elements) {
        return newList(elements);
    }
    public static <T> Boolean isNotEmpty(Collection<T> collection) {
        return !isEmpty(collection);
    }
    public static <T> List<T> newList(T... elements) {
        if (GeneralUtil.isNotNull(elements)) {
            ArrayList<T> arrayList = new ArrayList<T>(elements.length);
            arrayList.addAll(Arrays.asList(elements));
            return arrayList;
        }

        return new ArrayList<T>();
    }
    public static <T> Boolean isEmpty(Collection<T> collection) {
        if (GeneralUtil.isNull(collection))
            return Boolean.TRUE;

        return collection.isEmpty();
    }
}

Related Tutorials