[英]How can I find out the size of a java.util.List if the actual size is greater than the Integer.MAX_VALUE?
As I understood java.util.List
does not have size restrictions.据我了解java.util.List
没有大小限制。 As I understand, the size() method:据我了解, size() 方法:
Returns the number of elements in this list.返回此列表中的元素数。 If this list contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.如果此列表包含多个 Integer.MAX_VALUE 元素,则返回 Integer.MAX_VALUE。
Therefore I have following question:因此我有以下问题:
If my List
has a size that is larger than the Integer.MAX_VALUE
, is there a way to determine the exact size?如果我的List
的大小大于Integer.MAX_VALUE
,有没有办法确定确切的大小?
You would need to count them yourself, using a bigger data type.您需要使用更大的数据类型自己计算它们。
long realSize;
if (myList.size() < Integer.MAX_VALUE) {
realSize = myList.size();
} else {
realSize = Integer.MAX_VALUE;
ListIterator<Foo> iter = myList.listIterator(Integer.MAX_VALUE);
while (iter.hasNext()) {
realSize++;
iter.next();
}
}
If a list supports sizes that huge, it will almost certainly support some sort of alternate size
method you can use to get the size as a long.如果一个列表支持这么大的尺寸,它几乎肯定会支持某种替代size
方法,您可以使用它来获得很长的尺寸。 For example, the first such implementation I found, HugeCollections , has a longSize
method.例如,我发现的第一个这样的实现HugeCollections有一个longSize
方法。 The overhead of trying to get the true size without such a method is simply absurd.在没有这种方法的情况下试图获得真实大小的开销简直是荒谬的。
I don't think any standard library List implementation supports sizes that huge;我不认为任何标准库 List 实现支持这么大的大小; ArrayList and LinkedList don't. ArrayList 和 LinkedList 没有。 If you ever need to deal with this kind of problem, the documentation for the giant list type you're using should tell you what to do.如果您需要处理此类问题,您正在使用的巨型列表类型的文档应该会告诉您该怎么做。
It would depend on the specific implementation of List
you are using.这将取决于您使用的List
的具体实现。 For example ArrayList
uses a java array internally, which has a max size of Integer.MAX_VALUE - 5
.例如ArrayList
在内部使用 java 数组,其最大大小为Integer.MAX_VALUE - 5
。 Other implementations of List
might have different max sizes. List
其他实现可能具有不同的最大大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.