功能函数
创建ArrayList方法 |
newArrayList() newArrayList(E... elements) newArrayList(Iterable<? extends E> elements) newArrayList(Iterator<? extends E> elements) newArrayListWithCapacity(int initialArraySize) newArrayListWithExpectedSize(int estimatedSize) |
创建LinkedList方法 | newLinkedList() newLinkedList(Iterable<? extends E> elements) |
创建CopyOnWriteArrayList方法 | newCopyOnWriteArrayList() newCopyOnWriteArrayList(Iterable<? extends E> elements) |
创建自制List规则 | asList(@Nullable E first, E[] rest) asList(@Nullable E first, @Nullable E second, E[] rest) |
对多个List做笛卡尔乘积 | cartesianProduct(List<? extends List<? extends B>> lists) cartesianProduct(List<? extends B>... lists) |
从一个类型的List转到另一个类型的List方法 | transform(List<F> fromList, Function<? super F, ? extends T> function) |
对list进行分批输出的方法(作用之一:分页) | partition(List<T> list, int size) |
将字符串作为字符数组进行操作 | charactersOf(String string) charactersOf(CharSequence sequence) |
将list逆序 | reverse(List<T> list) |
PS:今天终于找到了插入代码块的地方哈哈哈哈
创建ArrayList方法
1、无参创建ArrayList
public static <E> ArrayList<E> newArrayList() {
return new ArrayList();
}
2、传入一个数组,返回一个ArrayList
public static <E> ArrayList<E> newArrayList(E... elements) {
//对数组进行判空处理
Preconditions.checkNotNull(elements);
// computeArrayListCapacity对当前数量进行优化
int capacity = computeArrayListCapacity(elements.length);
//这里根据优化后的数量进行创建一个新的ArrayList
ArrayList list = new ArrayList(capacity);
//将数组里面的元素都存储在List中
Collections.addAll(list, elements);
return list;
}
有上面,我们可以解析一下computeArrayListCapacity方法:
static int computeArrayListCapacity(int arraySize) {
//判断是否小于0,小于0则抛出异常
CollectPreconditions.checkNonnegative(arraySize, "arraySize");
// saturatedCast函数判断计算出来的数组长度是否在int的范围内
return Ints.saturatedCast(5L + (long)arraySize + (long)(ar