Java List Union unionOfListOfLists( final Collection> collectionOfCollection)

Here you can find the source of unionOfListOfLists( final Collection> collectionOfCollection)

Description

union Of List Of Lists

License

Open Source License

Parameter

Parameter Description
collectionOfCollection a Collection<Collection<T>>

Return

a Set<T> containing all values of all Collections<T> without any duplicates

Declaration

public static <T> Set<T> unionOfListOfLists(
        final Collection<? extends Collection<T>> collectionOfCollection) 

Method Source Code

//package com.java2s;
/**//from w ww  .j  a  v  a2 s. c o m
 * Copyright (C) 2013
 * by 52 North Initiative for Geospatial Open Source Software GmbH
 *
 * Contact: Andreas Wytzisk
 * 52 North Initiative for Geospatial Open Source Software GmbH
 * Martin-Luther-King-Weg 24
 * 48155 Muenster, Germany
 * info@52north.org
 *
 * This program is free software; you can redistribute and/or modify it under
 * the terms of the GNU General Public License version 2 as published by the
 * Free Software Foundation.
 *
 * This program is distributed WITHOUT ANY WARRANTY; even without the implied
 * WARRANTY OF MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program (see gnu-gpl v2.txt). If not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA or
 * visit the Free Software Foundation web page, http://www.fsf.org.
 */

import java.util.Collection;
import java.util.Collections;

import java.util.HashSet;

import java.util.Map;

import java.util.Set;

public class Main {
    /**
     * @param collectionOfCollection
     *            a Collection&lt;Collection&lt;T>>
     * 
     * @return a Set&lt;T> containing all values of all Collections&lt;T>
     *         without any duplicates
     */
    public static <T> Set<T> unionOfListOfLists(
            final Collection<? extends Collection<T>> collectionOfCollection) {
        if (collectionOfCollection == null
                || collectionOfCollection.isEmpty()) {
            return Collections.emptySet();
        }
        final HashSet<T> union = new HashSet<T>();
        for (final Collection<T> col : collectionOfCollection) {
            if (col != null) {
                for (final T t : col) {
                    if (t != null) {
                        union.add(t);
                    }
                }
            }
        }
        return union;
    }

    /**
     * Check if collection is not <tt>null</tt> and empty
     * 
     * @param collection
     *            Collection to check
     * 
     * @return <tt>true</tt>, if collection is not null and empty, else
     *         <tt>false</tt>
     */
    public static <T> boolean isEmpty(final Collection<T> collection) {
        return collection != null && collection.isEmpty();
    }

    /**
     * Check if map is not <tt>null</tt> and empty
     * 
     * @param map
     *            map to check
     * 
     * @return <tt>true</tt>, if map is not null and empty, else <tt>false</tt>
     */
    public static <K, V> boolean isEmpty(final Map<K, V> map) {
        return map != null && map.isEmpty();
    }
}

Related

  1. unionAdd(List vect, E obj)
  2. unionCreditList(List list)
  3. unionList( Collection col1, Collection col2)
  4. unionList(List la, List lb)
  5. unionList(List list1, List list2)
  6. unionSeparator(List elements, String separator)