Is there a longer than int Java List?

后端 未结 3 859
谎友^
谎友^ 2020-12-19 16:47

I can\'t seem to find a Java List that\'s max length is long\'s max value.

Does such a List exist?

If so, where?

3条回答
  •  北海茫月
    2020-12-19 17:34

    As @afsantos says, the ArrayList class is inherently limited to Integer.MAX_VALUE entries because of the limitations of Java arrays.

    LinkedList doesn't have this limitation, but it is (nonetheless) expensive:

    • Each entry incurs an memory overhead of 2 references plus the size of an object header ... compared to just one reference for an array-based representation.

    • Indexing is an O(N) operation compared with O(1) for an array-based list.

    Here is a link to Java library that supports huge in-memory collections using direct mapped memory and/or encoding of the elements:

    • http://code.google.com/p/vanilla-java/wiki/HugeCollections

    There could be other alternatives out there.

    One could also envisage a "big" variant of regular array lists that used an array of arrays rather than a single array. But if you allow insertion into the middle of the list, it becomes difficult / expensive to achieve O(1) lookup. (That might be why I couldn't find an example with Google ...)

提交回复
热议问题