Java List Single Value singletonList(T element)

Here you can find the source of singletonList(T element)

Description

Replies a singleton list with the given element, or the empty list if the element is null.

License

Apache License

Parameter

Parameter Description
T the type of the element in the list.
element the element.

Return

the list with the element, or the empty list.

Declaration

public static <T> List<T> singletonList(T element) 

Method Source Code

//package com.java2s;
/*/*from  www .  jav a2  s  . co m*/
 * $Id$
 *
 * SARL is an general-purpose agent programming language.
 * More details on http://www.sarl.io
 *
 * Copyright (C) 2014-2016 the original authors or authors.
 *
 * 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.Collections;

import java.util.List;

public class Main {
    /** Replies a singleton list with the given element, or the empty list if
     * the element is <code>null</code>.
     *
     * @param <T> the type of the element in the list.
     * @param element the element.
     * @return the list with the element, or the empty list.
     */
    public static <T> List<T> singletonList(T element) {
        if (element == null) {
            return Collections.emptyList();
        }
        return Collections.singletonList(element);
    }
}

Related

  1. single(List list)
  2. singleElementList(T element)
  3. singleItemAsList(T item)
  4. singleQuoatanizeStringList(List entries)
  5. singletonList(final T source)
  6. singletonListIfNotNullOrEmptyListIfNull(T element)
  7. singleValue(Class type, List values)
  8. singleValue(List l)
  9. singleValue(List values)