merge two Collection - Java java.util

Java examples for java.util:Collection Operation

Description

merge two Collection

Demo Code


//package com.java2s;
import java.util.Collection;

public class Main {
    public static void main(String[] argv) {
        Collection c1 = java.util.Arrays.asList("asdf", "java2s.com");
        Collection c2 = java.util.Arrays.asList("asdf", "java2s.com");
        System.out.println(merge(c1, c2));
    }/* ww  w.  ja v  a 2 s  .c  om*/

    public static <T> Collection<T> merge(Collection<T> c1, Collection<T> c2) {
        for (T e : c2) {
            if (!c1.contains(e)) {
                c1.add(e);
            }
        }
        return c1;
    }
}

Related Tutorials