Java ArrayList Create newArrayListSized(Iterable fromSize)

Here you can find the source of newArrayListSized(Iterable fromSize)

Description

Creates a new ArrayList which if possible is sized the same as the passed in iterable (ie only if it is actually a Collection).

License

Open Source License

Declaration

public static <T> ArrayList<T> newArrayListSized(Iterable<?> fromSize) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    /**//from w ww  . j  a  va  2s  .  c  om
     * Creates a new ArrayList which if possible is sized the same as the passed in
     * iterable (ie only if it is actually a Collection). 
     */
    public static <T> ArrayList<T> newArrayListSized(Iterable<?> fromSize) {
        if (fromSize instanceof Collection<?>)
            return new ArrayList<T>(((Collection<?>) fromSize).size());
        else
            return new ArrayList<T>();
    }
}

Related

  1. newArrayList(T... items)
  2. newArrayList(T... objectList)
  3. newArrayList(T... objs)
  4. newArrayList(X... args)
  5. newArrayListOnNull(List list)
  6. newArrayListWithCapacity(int initSize)
  7. newArrayOfEmptyArrayList(int len)
  8. sizedArrayList(final int size)
  9. sizedArrayList(int capacity)