MAGIC number in Android google io 2011 java source

The below code is captured by the open source google io.

com.google.android.apps.iosched.util.Lists.java http://code.google.com/p/iosched/source/browse/android/src/com/google/android/apps/iosched/util/Lists .java

public static <E> ArrayList<E> newArrayList(E... elements) { int capacity = (elements.length * 110) / 100 + 5; ArrayList<E> list = new ArrayList<E>(capacity); Collections.addAll(list, elements); return list; } 

com.google.android.apps.iosched.util.Sets.java http://code.google.com/p/iosched/source/browse/android/src/com/google/android/apps/iosched/util/Sets .java

 public static <E> HashSet<E> newHashSet(E... elements) { int capacity = elements.length * 4 / 3 + 1; HashSet<E> set = new HashSet<E>(capacity); Collections.addAll(set, elements); return set; } 

What should be the power variable? Thanks in advance!

+4
source share
2 answers

These collections internally use a fixed array to store data. "Capacity" is the initial number of elements that an array can hold. When you add more items than the current capacity, the inner array needs to be expanded. This is a time-consuming operation, and the initial capacity is trying to help if you know how many items will be added.

+2
source

This is part of the ArrayList class. Setting preset capacity does not allow large lists to increase their size as they are filled and instead allocates the necessary space immediately.

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/ArrayList.html

+1
source

Source: https://habr.com/ru/post/1403152/


All Articles