Inserts into one collection all elements of another collection not contained in that collection. - Android java.util

Android examples for java.util:Collection Contains

Description

Inserts into one collection all elements of another collection not contained in that collection.

Demo Code


//package com.book2s;

import java.util.Collection;

public class Main {
    public static void main(String[] argv) {
        Collection src = java.util.Arrays.asList("asdf", "book2s.com");
        Collection target = java.util.Arrays.asList("asdf", "book2s.com");
        addNotContainedElements(src, target);
    }/*from w  w w. ja  v a 2  s  .  c o  m*/

    /**
     * Inserts into one collection all elements of another collection not
     * contained in that collection.
     * <p>
     * Uses {@code Collection#contains(java.lang.Object)} to compare elements.
     *
     * @param <T>    the collection's element type
     * @param src    source collection to get elements from
     * @param target target collection to put elements into
     */
    public static <T> void addNotContainedElements(
            Collection<? extends T> src, Collection<? super T> target) {
        if (src == target) {
            return;
        }

        for (T t : src) {
            if (!target.contains(t)) {
                target.add(t);
            }
        }
    }
}

Related Tutorials