Java tutorial
//package com.java2s; /* ============== HelperTools ============== * Initial developer: Lukas Diener <lukas.diener@hotmail.com> * * ===== * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE - Version 2 * * Everyone is permitted to copy and distribute verbatim or modified * copies of this license document, and changing it is allowed as long * as the name is changed. * * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE * TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION * * 0. You just DO WHAT THE FUCK YOU WANT TO. * * ===== * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND. * */ import java.util.Collection; import java.util.HashSet; import java.util.Set; public class Main { /** * This method performs a union on two collections of elements as per definition of the <i>union</i> operation in set theory. * The resulting collection guarantees unique membership as per implementation of the Java {@link java.util.Set} interface. * This is a null-safe operation. * @see <a href="http://en.wikipedia.org/wiki/Union_(set_theory)">Union (set theory)</a> * @param firstGroup Collection to be used. * @param secondGroup Collection to be used. * @return The resulting collection containing all distinct members from the two groups. */ public static <T> Collection<T> union(Collection<T> firstGroup, Collection<T> secondGroup) { if (firstGroup == null) return secondGroup; else if (secondGroup == null) return firstGroup; else { Set<T> firstSet = new HashSet<T>(firstGroup); firstSet.addAll(secondGroup); return firstSet; } } }