Java List Value Add add(List list, E element)

Here you can find the source of add(List list, E element)

Description

Returns the given List instance or a new List instance if the given one is null.

License

Apache License

Parameter

Parameter Description
list a parameter
element a parameter

Return

given instance or new instance if given instance is null

Declaration

public static <E> List<E> add(List<? extends E> list, E element) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2011 Danny Kunz/*  w w w.j  a v  a 2  s.co  m*/
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Returns the given {@link List} instance or a new {@link List} instance if the given one is null. The returned instance will
     * contain all elements of the given {@link List} and additionally the further given element. <br>
     * <br>
     * This function will return always an instance, even if the given list is null.
     * 
     * @param list
     * @param element
     * @return given {@link List} instance or new {@link List} instance if given {@link List} instance is null
     */
    public static <E> List<E> add(List<? extends E> list, E element) {
        //
        @SuppressWarnings("unchecked")
        List<E> retlist = (List<E>) list;

        //
        if (list == null) {
            retlist = new ArrayList<E>();
        }

        //
        retlist.add(element);

        //
        return retlist;
    }

    /**
     * Similar to {@link #add(List, Object)} allowing to specify an index position
     * 
     * @param list
     * @param index
     * @param element
     * @return the given {@link List} instance or a new one
     */
    public static <E> List<E> add(List<? extends E> list, int index, E element) {
        //
        @SuppressWarnings("unchecked")
        List<E> retlist = (List<E>) list;

        //
        if (list == null) {
            retlist = new ArrayList<E>();
        }

        //
        while (list.size() < index) {
            list.add(null);
        }

        //
        retlist.add(index, element);

        //
        return retlist;
    }

    /**
     * Returns the given {@link List} instance or a new {@link List} instance if the given one is null. The returned instance will
     * contain all elements of the given {@link List} and additionally all further given elements. <br>
     * <br>
     * This function will return always a instance, even if the given list is null.
     * 
     * @param list
     * @param elements
     * @return given {@link List} instance or new {@link List} instance if given {@link List} instance is null
     */
    public static <E> List<E> add(List<? extends E> list, E... elements) {
        //
        @SuppressWarnings("unchecked")
        List<E> retlist = (List<E>) list;

        //
        if (list == null) {
            retlist = new ArrayList<E>();
        }

        //
        if (elements != null) {
            for (E element : elements) {
                retlist.add(element);
            }
        }

        //
        return retlist;
    }

    /**
     * Similar to {@link #add(List, Object...)} allowing to specify an index position
     * 
     * @param list
     * @param index
     * @param elements
     * @return the given {@link List} instance or a new one
     */
    public static <E> List<E> add(List<? extends E> list, int index, E... elements) {
        //
        @SuppressWarnings("unchecked")
        List<E> retlist = (List<E>) list;

        //
        if (list == null) {
            retlist = new ArrayList<E>();
        }

        //
        while (list.size() < index) {
            list.add(null);
        }

        //
        int relative = 0;
        if (elements != null) {
            for (E element : elements) {
                retlist.add(index + relative, element);
                relative++;
            }
        }

        //
        return retlist;
    }
}

Related

  1. add(final Integer id, final TreeMap> map, T object)
  2. add(final List list, final T... objects)
  3. add(final List toList, final List fromList)
  4. add(List list, int index, Object element)
  5. add(List valList, Object src)
  6. add(List result, ClassLoader loader)
  7. add(List list, E element)
  8. add(List list, int index, T item)
  9. add(List list, int index, T item)