Merge an array to specified collection - Android java.util

Android examples for java.util:Collection Merge

Description

Merge an array to specified collection

Demo Code


import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.elminster.common.util.Messages.Message;

public class Main{
    public static void main(String[] argv){
        Collection collection = java.util.Arrays.asList("asdf","book2s.com");
        Object array = "book2s.com";
        mergeArray(collection,array);//from w w  w  .j ava  2  s .com
    }
    /**
     * Merge an array to specified collection
     * 
     * @param collection
     *          specified collection
     * @param array
     *          an array
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static void mergeArray(Collection collection, Object array) {
        if (null == collection) {
            throw new IllegalArgumentException(
                    Messages.getString(Message.COLLECTION_IS_NULL));
        }
        Object[] arr = ObjectUtil.toObjectArray(array);
        for (Object elem : arr) {
            collection.add(elem);
        }
    }
}

Related Tutorials