Java Collection Concatenate concat(Collection... collections)

Here you can find the source of concat(Collection... collections)

Description

concat

License

Apache License

Declaration

public static <T> Collection<T> concat(Collection<T>... collections) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

import java.util.List;

public class Main {

    public static <T> Collection<T> concat(Collection<T>... collections) {
        if (collections == null || collections.length == 0) {
            return Collections.emptyList();
        }//from   w  w w  .j av  a 2 s. co m
        if (collections.length == 1) {
            return collections[0];
        }
        return concat(Arrays.asList(collections));
    }

    public static <T> Collection<T> concat(Collection<? extends Collection<T>> collections) {
        int size = 0;
        for (Collection<T> c : collections) {
            if (c != null) {
                size += c.size();
            }
        }
        List<T> result = new ArrayList<T>(size);
        for (Collection<T> c : collections) {
            if (c != null) {
                result.addAll(c);
            }
        }
        return result;
    }
}

Related

  1. concat(Collection parts, int start, String defaultText)
  2. concat(Collection pieces, String delim)
  3. concat(Collection a, Collection b)
  4. concat(Collection a, int offset, String sep, String start, String end)
  5. concat(Collection first, Collection second)
  6. concat(final Collection... collections)
  7. concat(final Collection... collections)
  8. concat(String separator, Collection args)
  9. concat(String[] first, Collection second)