Java Collection Merge merge(Collection... collections)

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

Description

Merges multiple collections into a list with natural ordering of elements

License

Open Source License

Parameter

Parameter Description
collections the collections
T the element type

Return

the merged list

Declaration

public static <T extends Comparable> List<T> merge(Collection<T>... collections) 

Method Source Code

//package com.java2s;
/**/*from   ww w  . j  a  v a  2 s . c  o m*/
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://license.openmrs.org
 * <p>
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 * <p>
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */

import java.util.*;

public class Main {
    /**
     * Merges multiple collections into a list with natural ordering of elements
     *
     * @param collections the collections
     * @param <T>         the element type
     * @return the merged list
     */
    public static <T extends Comparable> List<T> merge(Collection<T>... collections) {
        Set<T> merged = new TreeSet<T>();

        for (Collection<T> list : collections) {
            for (T element : list) {
                merged.add(element);
            }
        }

        return new ArrayList(merged);
    }
}

Related

  1. merge(CC target, Collection a, Collection b)
  2. merge(Collection dest, Collection src)
  3. merge(Collection splits)
  4. merge(Collection strings, char separator)
  5. merge(Collection... collections)
  6. merge(Collection... elems)
  7. merge(final Collection values)
  8. merge(J just, Collection justs)
  9. mergeArrayIntoCollection(T[] array, Collection collection)