Java List from Array asList(final T[] array)

Here you can find the source of asList(final T[] array)

Description

Converts the given array to a List.

License

Open Source License

Parameter

Parameter Description
T type of objects contained in the array
array array to convert

Return

list (Collections.EMPTY_LIST if the array is empty)

Declaration

public static <T> List<T> asList(final T[] array) 

Method Source Code


//package com.java2s;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

import java.util.List;
import java.util.Map;

public class Main {
    /**//  w w w.j  a v  a  2 s .  co m
     * Converts the given array to a List. <br/>
     * If the array is empty, it gives the Collections.EMPTY_LIST
     *
     * @param <T> type of objects contained in the array
     * @param array array to convert
     * @return list (Collections.EMPTY_LIST if the array is empty)
     * @see java.util.Arrays#asList(Object...)
     */
    public static <T> List<T> asList(final T[] array) {
        List<T> res;
        if (isEmpty(array)) {
            res = Collections.emptyList();
        } else {
            res = Arrays.asList(array);
        }
        return res;
    }

    /**
     * Is the given map null or empty ?
     *
     * @param map map to test
     * @return true if the map is null or empty
     */
    public static boolean isEmpty(final Map<?, ?> map) {
        return map == null || map.isEmpty();
    }

    /**
     * Is the given collection null or empty ?
     *
     * @param col collection to test
     * @return true if the collection is null or empty
     */
    public static boolean isEmpty(final Collection<?> col) {
        return col == null || col.isEmpty();
    }

    /**
     * Is the given array null or empty ?
     *
     * @param array array to test
     * @return true if the array is null or empty
     */
    public static boolean isEmpty(final Object[] array) {
        return array == null || array.length == 0;
    }
}

Related

  1. asList(final Set elements)
  2. asList(final T node)
  3. asList(final T... array)
  4. asList(final T... data)
  5. asList(final T... data)
  6. asList(final T[] input)
  7. asList(float[] elements)
  8. asList(int... array)
  9. asList(int... data)