union two Collection - Android java.util

Android examples for java.util:Collection Union

Description

union two Collection

Demo Code


//package com.book2s;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

public class Main {
    public static void main(String[] argv) {
        Collection l1 = java.util.Arrays.asList("asdf", "book2s.com");
        Collection l2 = java.util.Arrays.asList("asdf", "book2s.com");
        System.out.println(union(l1, l2));
    }//from  ww  w  .ja  v a2 s  . c o  m

    public static <T> ArrayList<T> union(Collection<T> l1, Collection<T> l2) {
        Set<T> s = new HashSet<>();
        s.addAll(l1);
        s.addAll(l2);
        return new ArrayList<>(s);
    }
}

Related Tutorials