Java List from Array asList(final Collection collection)

Here you can find the source of asList(final Collection collection)

Description

as List

License

Open Source License

Declaration

@SuppressWarnings("unchecked")
    public static <T> List<T> asList(final Collection<? extends T> collection) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010-2014 Alexander Kerner. All rights reserved.
 *
 * 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./*from   w w  w.  j av  a2  s.  c  o m*/
 ******************************************************************************/

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

import java.util.List;

public class Main {
    @SuppressWarnings("unchecked")
    public static <T> List<T> asList(final Collection<? extends T> collection) {
        List<T> result;
        if (collection instanceof List) {
            result = (List<T>) collection;
        } else {
            result = newList(collection);
        }
        return result;
    }

    public static <T> List<T> newList() {
        return new ArrayList<T>();
    }

    public static <T> List<T> newList(final Collection<? extends T> elements) {
        return new ArrayList<T>(elements);
    }

    public static <T> List<T> newList(final Collection<? extends T>... elements) {
        final List<T> result = newList();
        for (final Collection<? extends T> c : elements) {
            result.addAll(c);
        }
        return result;
    }

    public static <T> List<T> newList(final T... elements) {
        return Arrays.asList(elements);
    }
}

Related

  1. asList(double[] array)
  2. asList(E... elements)
  3. asList(E... pEntities)
  4. asList(E[] array)
  5. asList(final char[] array)
  6. asList(final Collection c)
  7. asList(final E... array)
  8. asList(final int... values)
  9. asList(final int[] a)