Java Array to List toList(final T[] array)

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

Description

Creates a modifiable list with all of the items of the provided array in the same order.

License

Open Source License

Parameter

Parameter Description
T The type of item contained in the provided array.
array The array of items to include in the list.

Return

The list that was created, or null if the provided array was null .

Declaration

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

Method Source Code

//package com.java2s;
/*/*  w w  w  .j a  v a  2 s . co  m*/
 * Copyright (C) 2008-2018 Ping Identity Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License (GPLv2 only)
 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 */

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

import java.util.List;

public class Main {
    /**
     * Creates a modifiable list with all of the items of the provided array in
     * the same order.  This method behaves much like {@code Arrays.asList},
     * except that if the provided array is {@code null}, then it will return a
     * {@code null} list rather than throwing an exception.
     *
     * @param  <T>  The type of item contained in the provided array.
     *
     * @param  array  The array of items to include in the list.
     *
     * @return  The list that was created, or {@code null} if the provided array
     *          was {@code null}.
     */
    public static <T> List<T> toList(final T[] array) {
        if (array == null) {
            return null;
        }

        final ArrayList<T> l = new ArrayList<>(array.length);
        l.addAll(Arrays.asList(array));
        return l;
    }
}

Related

  1. toList(final Object[] objects)
  2. toList(final T... objects)
  3. toList(final T[] array)
  4. toList(final T[] array)
  5. toList(final T[] array)
  6. toList(final T[] array)
  7. toList(int[] a)
  8. toList(int[] arr)
  9. toList(int[] array)