Lists.newArrayListWithExpectedSize( int estimatedSize)


Lists.newArrayListWithExpectedSize( int estimatedSize)  构造一个期望长度为estimatedSizeArrayList实例。

源码:

 

public static <E> ArrayList<E> newArrayListWithExpectedSize(int estimatedSize) {
    return new ArrayList(computeArrayListCapacity(estimatedSize));
}

 

//计算集合的容量
@VisibleForTesting
static int computeArrayListCapacity(int arraySize) {
//判断是否小于0,小于0则抛出异常。
    CollectPreconditions.checkNonnegative(arraySize, "arraySize");
    return Ints.saturatedCast(5L + (long)arraySize + (long)(arraySize / 10));
}
//检查是否小于0
@CanIgnoreReturnValue
static int checkNonnegative(int value, String name) {
    if (value < 0) {
        throw new IllegalArgumentException(name + " cannot be negative but was: " + value);
    } else {
        return value;
    }
}
//判断计算出来的数组长度是否在int的范围内
public static int saturatedCast(long value) {
    if (value > 2147483647L) {
        return 2147483647;
    } else {
        return value < -2147483648L ? -2147483648 : (int)value;
    }
}

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM