Is there a longer than int Java List?

女生的网名这么多〃 提交于 2019-11-30 09:08:57

问题


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?


回答1:


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 ...)




回答2:


From the List documentation:

int size()

Returns the number of elements in this list. If this list contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

So, even if a specific implementation of List holds Long.MAX_VALUE elements, you wouldn't know, using the standard List interface.

I'm not sure if one exists, but my bet would be on LinkedList, since ArrayList is based on arrays, and those can't hold more than Integer.MAX_VALUE elements.




回答3:


As List.get(int) accepts int as its argument, it is impossible to address entries with indexes greater than Integer.MAX_VALUE.

Do note, however, that Iterable<?> or Map<Long, ?> can address much more data. As List implements Iterable it could contain any amount of data (that part being not exposed with int-based List's API).



来源:https://stackoverflow.com/questions/21371877/is-there-a-longer-than-int-java-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!