This code is a simple guava library code.
I simplified the simplified reading, the original code see => link
// Case A public static <E> ArrayList<E> newArrayList(E... elements) { int capacity = computeArrayListCapacity(elements.length); ArrayList<E> list = new ArrayList<E>(capacity); Collections.addAll(list, elements); return list; } static int computeArrayListCapacity(int arraySize) { long value = 5L + arraySize + (arraySize / 10); if (value > Integer.MAX_VALUE) { return Integer.MAX_VALUE; } if (value < Integer.MIN_VALUE) { return Integer.MIN_VALUE; } return (int) value; }
Why set the capacity to 5L + arraySize + (arraySize / 10) ?
And what is different between 3 cases (A, B, C)?
//Case B public static <E> ArrayList<E> newArrayList(E... elements) { ArrayList<E> list = new ArrayList<E>(elements.length); Collections.addAll(list, elements); return list; } //Case C public static <E> ArrayList<E> newArrayList(E... elements) { ArrayList<E> list = new ArrayList<E>(); Collections.addAll(list, elements); return list; }
source share