Convert Array as List - Java Collection Framework

Java examples for Collection Framework:Array Convert

Description

Convert Array as List

Demo Code


//package com.java2s;
import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

public class Main {
    @SafeVarargs/* www .  ja v a  2s  .c o  m*/
    public static <E> List<E> asList(E... elements) {
        if (elements == null || elements.length == 0) {
            return Collections.emptyList();
        }
        int capacity = computeListCapacity(elements.length);
        ArrayList<E> list = new ArrayList<E>(capacity);
        Collections.addAll(list, elements);
        return list;
    }

    static int computeListCapacity(int arraySize) {
        return (int) Math.min(5L + arraySize + (arraySize / 10),
                Integer.MAX_VALUE);
    }
}

Related Tutorials