Java List Value Add All addToList(List list, T value)

Here you can find the source of addToList(List list, T value)

Description

Check and adds elements to list (create new ArrayList if list is null)

License

Open Source License

Parameter

Parameter Description
T the generic type
list the list
value the value

Return

the list (if list is null, create new array list)

Declaration

public static <T> List<T> addToList(List<T> list, T value) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**/*from  w w w . j  av  a2  s .  c  o m*/
     * Check and adds elements to list (create new ArrayList if list is null)
     *
     * @param <T>
     *            the generic type
     * @param list
     *            the list
     * @param value
     *            the value
     * @return the list (if list is null, create new array list)
     */
    public static <T> List<T> addToList(List<T> list, T value) {
        if (value == null)
            return list;
        if (list == null)
            list = new ArrayList<T>();
        list.add(value);
        return list;
    }
}

Related

  1. addArrayToList(T[] array, List list)
  2. addToList(List list, Object obj)
  3. addToList(List list, I item)
  4. AddToList(List tips, String tip)
  5. addToList(List dest, T src)
  6. addToList(List list, T[] array)
  7. addToList(List list, U... items)