Adds all of the elements in the specified collection to the target collection if a collection is specified. - Java java.util

Java examples for java.util:Collection Element

Description

Adds all of the elements in the specified collection to the target collection if a collection is specified.

Demo Code


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

public class Main {
    public static void main(String[] argv) {
        Collection target = java.util.Arrays.asList("asdf", "java2s.com");
        Collection c = java.util.Arrays.asList("asdf", "java2s.com");
        System.out.println(addAllIfSet(target, c));
    }//  w  w w  . j  a v a2 s.  co  m

    /**
     * Adds all of the elements in the specified collection to the target collection if
     * a collection is specified.
     * 
     * @param <E>
     * @param target
     * @param c collection containing elements to be added to this collection, may be null
     * @return <tt>true</tt> if this collection changed as a result of the call
     */
    public static <E> boolean addAllIfSet(Collection<E> target,
            Collection<? extends E> c) {
        if (c == null)
            return false;
        return target.addAll(c);
    }
}

Related Tutorials