Google Guava is a java library with lot of utilities and reusable components. This requires the library guava-10.0.jar to be in classpath. The following example shows using Sets.union() API. It returns an unmodifiable view of the union of two sets.
package com.bethecoder.tutorials.guava.collection_tests; import java.util.Set; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Sets; public class SetsUnionTest { /** * @param args */ public static void main(String[] args) { ImmutableSet<String> stringSet = ImmutableSet.of("ONE", "TWO", "THREE"); System.out.println(stringSet); ImmutableSet<String> stringSet2 = ImmutableSet.of("TWO", "THREE", "FOUR"); System.out.println(stringSet2); Set<String> resultSet = Sets.union(stringSet, stringSet2); System.out.println(resultSet); } }
[ONE, TWO, THREE] [TWO, THREE, FOUR] [ONE, TWO, THREE, FOUR]