简体   繁体   English

如果实际大小大于 Integer.MAX_VALUE,如何找出 java.util.List 的大小?

[英]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.

相关问题 如何知道float值是否大于Integer.MAX_VALUE? - How to know if the float value is greater than Integer.MAX_VALUE? 为什么Java数组的最大大小为Integer.MAX_VALUE / 7? - Why maximum size of an java array is Integer.MAX_VALUE/7? Spark Java错误:大小超过Integer.MAX_VALUE - Spark Java Error: Size exceeds Integer.MAX_VALUE 如果列表中的元素数量大于 Integer.MAX_VALUE,LinkedList 是否违反合同? - Is LinkedList breaks contract if amount of elements in list greater than Integer.MAX_VALUE? 如果元素总数大于Integer.MAX_VALUE,我们可以使用IntStream#sum吗? - Can we use IntStream#sum, If sum of elements is greater than the Integer.MAX_VALUE? 可以String保持大于Integer.MAX_VALUE个字符数 - Can String hold greater than Integer.MAX_VALUE number of character Java中的Integer.MAX_VALUE - Integer.MAX_VALUE in java 为什么ArrayList的最大数组大小是Integer.MAX_VALUE - 8? - Why the maximum array size of ArrayList is Integer.MAX_VALUE - 8? Circumvent数组大小&gt; BufferedImage的Integer.MAX_VALUE? - Circumvent Array size > Integer.MAX_VALUE for BufferedImage? 嵌套异常为java.lang.IllegalArgumentException:大小不能大于Integer最大值:5410783931 - nested exception is java.lang.IllegalArgumentException: Size cannot be greater than Integer max value: 5410783931
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM