add All Not Exists from one Collection to another Collection - Java java.util

Java examples for java.util:Collection Element

Description

add All Not Exists from one Collection to another Collection

Demo Code


//package com.book2s;

import java.util.Collection;
import java.util.Iterator;

public class Main {
    public static void main(String[] argv) {
        Collection c1 = java.util.Arrays.asList("asdf", "book2s.com");
        Collection c2 = java.util.Arrays.asList("asdf", "book2s.com");
        addAllNotExists(c1, c2);/*from w w  w.j  a  v  a  2s.  c  om*/
    }

    public static void addAllNotExists(Collection c1, Collection c2) {
        for (Iterator iterator = c2.iterator(); iterator.hasNext();) {
            addNotExists(c1, iterator.next());
        }
    }

    public static void addNotExists(Collection<Object> c, Object obj) {
        if (!c.contains(obj)) {
            c.add(obj);
        }
    }
}

Related Tutorials