Java Collection Merge mergeCollection( final Collection col1, final Collection col2)

Here you can find the source of mergeCollection( final Collection col1, final Collection col2)

Description

Merge collections where all objects are of the same type.

License

Open Source License

Parameter

Parameter Description
col1 a parameter
col2 a parameter

Declaration

public static <T> Collection<T> mergeCollection(
        final Collection<T> col1, final Collection<T> col2) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004-2013/*  ww  w  .  j  av a 2  s  .c  o  m*/
 * Contributors: L. Armanet, M. Camerlenghi, L. Cardonne, S. Delageniere,
 *               L. Duparchy, S. Ohlsson, P. Pascal, I. Schneider, S.Schulze,
 *               F. Torres
 * 
 * This file is part of the MIS tools package.
 * 
 * The MIS tools package is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * The MIS tools package is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with the MIS tools package.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

import java.util.ArrayList;

import java.util.Collection;

public class Main {
    /**
     * Merge collections where all objects are of the same type.
     * 
     * @param col1
     * @param col2
     * @return
     */
    public static <T> Collection<T> mergeCollection(
            final Collection<T> col1, final Collection<T> col2) {
        if (col1 == null) {
            if (col2 == null) {
                return null;
            }
            return col2;
        }
        if (col2 == null) {
            return col1;
        }
        Collection<T> result = new ArrayList<T>(col1.size() + col2.size());
        result.addAll(col1);
        result.addAll(col2);
        return result;
    }
}

Related

  1. merge(Collection... collections)
  2. merge(Collection... elems)
  3. merge(final Collection values)
  4. merge(J just, Collection justs)
  5. mergeArrayIntoCollection(T[] array, Collection collection)
  6. mergeCollection(Collection a, Collection b)
  7. mergeCollections(Collection... inCollections)