超越 Java 中的 Integer.MAX_VALUE 约束
抛开堆的容量不谈,有没有办法超越 Java 中的 Integer.MAX_VALUE 限制?
示例包括:
- 集合将自身限制为 Integer.MAX_VALUE。
- StringBuilder / StringBuffer 将自身限制为 Integer.MAX_VALUE。
Setting aside the heap's capacity, are there ways to go beyond Integer.MAX_VALUE constraints in Java?
Examples are:
- Collections limit themselves to Integer.MAX_VALUE.
- StringBuilder / StringBuffer limit themselves to Integer.MAX_VALUE.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您有一个巨大的集合,那么在您拥有 231 - 1 个项目。 一个包含 100 万件物品的集合将非常笨重,更不用说一个数量超过数千倍的集合了。
同样,StringBuilder 可以在达到 MAX_VALUE 限制之前构建 2GB 大小的字符串,这对于任何实际目的来说都绰绰有余。
如果您确实认为您可能会达到这些限制,您的应用程序应该以不同的方式存储数据,可能是在数据库中。
If you have a huge Collection you're going to hit all sorts of practical limits before you ever have 231 - 1 items in it. A Collection with a million items in it is going to be pretty unwieldy, let alone one with more than a thousands times more than that.
Similarly, a StringBuilder can build a String that's 2GB in size before it hits the
MAX_VALUE
limit which is more than adequate for any practical purpose.If you truly think that you might be hitting these limits your application should be storing your data in a different way, probably in a database.
带长? 对我有用。
编辑:啊,问题的澄清。 凉爽的。 我的新的和改进的答案:
使用分页算法。
巧合的是,最近另一个问题(在java中的排序(内存映射?)文件中进行二进制搜索),我创建了一个分页算法来绕过 java.nio.MappedByteBuffer API 中的 int 参数。
With a long? Works for me.
Edit: Ah, clarification of the question. Cool. My new and improved answer:
With a paging algorithm.
Coincidentally, somewhat recently for another question (Binary search in a sorted (memory-mapped ?) file in java), I whipped up a paging algorithm to get around the int parameters in the java.nio.MappedByteBuffer API.
您可以根据这些集合的源代码创建自己的具有长 size() 的集合。 例如,要拥有更大的对象数组,您可以拥有一个数组数组(并将它们缝合在一起),
这种方法将允许几乎 2^62 个元素。
You can create your own collections which have a long size() based on the source code for those collections. To have larger arrays of Objects for example, you can have an array of arrays (and stitch these together)
This approach will allow almost 2^62 elements.
数组索引受 Integer.MAX_VALUE 限制,而不是数组的物理大小。
因此,数组的最大大小与数组类型的大小相关。
字典则不同,因为它们经常使用存储桶或内部数据布局等技术作为树。 因此,这些“限制”通常不适用,或者您将需要更多数据才能达到限制。
简而言之:Integer.MAX_VALUE 并不是真正的限制,因为您需要大量内存才能真正达到限制。 如果您达到此限制,您可能需要考虑改进您的算法和/或数据布局:)
Array indexes are limited by Integer.MAX_VALUE, not the physical size of the array.
Therefore the maximum size of an array is linked to the size of the array-type.
Dictionaries are a different story because they often use techniques like buckets or an internal data layout as a tree. Therefore these "limits" usually dont apply or you will need even more data to reach the limit.
Short: Integer.MAX_VALUE is not really a limit because you need lots of memory to actually reach the limit. If you should ever reach this limit you might want to think about improving your algorithm and/or data-layout :)
是的,使用 BigInteger 类。
Yes, with BigInteger class.
内存升级是必要的..:)
A memory upgrade is necessary.. :)