Java List Null Empty newListWithOneNull()

Here you can find the source of newListWithOneNull()

Description

new List With One Null

License

Open Source License

Declaration

public static <T> ArrayList<T> newListWithOneNull() 

Method Source Code

//package com.java2s;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

public class Main {
    public static <T> ArrayList<T> newListWithOneNull() {
        ArrayList<T> newList = new ArrayList<T>();
        newList.add(null);// ww  w  . jav  a 2  s . c o m
        return newList;
    }

    public static <T, C extends Collection<T>> C add(C coll, T... items) {
        return plus(coll, items);
    }

    public static <T, C extends Collection<T>, D extends Collection<T>> C add(C coll, D items) {
        return plus(coll, items);
    }

    public static <T, C extends Collection<T>> C plus(C coll, T... items) {
        coll.addAll(newList(items));
        return coll;
    }

    public static <T, C extends Collection<T>, D extends Collection<T>> C plus(C coll, D items) {
        coll.addAll(items);
        return coll;
    }

    /**
     * A potentially more efficient addAll() for unordered Collections.
     * @param coll1
     * @param coll2
     * @return the longer of the two collections after adding the shorter to the longer.  
     */
    public static <T, C extends Collection<T>> C addAll(Collection<T> coll1, Collection<T> coll2) {
        if (coll1 == null)
            return (C) coll2;
        if (coll2 == null)
            return (C) coll1;

        Collection<T> cSmaller, cBigger;
        if (coll1.size() < coll2.size()) {
            cSmaller = coll1;
            cBigger = coll2;
        } else {
            cSmaller = coll2;
            cBigger = coll1;
        }
        try {
            cBigger.addAll(cSmaller);
            return (C) cBigger;
        } catch (UnsupportedOperationException e) {
        }
        try {
            cSmaller.addAll(cBigger);
            return (C) cSmaller;
        } catch (UnsupportedOperationException e) {
        }
        ArrayList<T> newList = new ArrayList<T>(cBigger);
        newList.addAll(cSmaller);
        return (C) newList;
    }

    /**
     * Creates a new {@link ArrayList} and inserts the arguments, {@code ts}.
     * @param ts
     * @return the new {@link ArrayList}
     */
    public static <T> ArrayList<T> newList(T... ts) {
        ArrayList<T> newList = new ArrayList<T>();
        if (ts != null && ts.length > 0) {
            newList.addAll(Arrays.asList(ts));
        }
        return newList;
    }
}

Related

  1. listWithoutNull(A... elements)
  2. listWithoutNull(final List list)
  3. maskNull(List pTypes)
  4. newListIfNull(List list)
  5. newListWhenNull(List list)
  6. nullOrEmpty(List s)
  7. nullOrEmptyList(List list)
  8. nullSafeAdd(List list, T elem)
  9. nullsafeAsList(T... elements)