我一直只是一个人使用:
List<String> names = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)
我使用接口作为可移植性的类型名称,因此当我问这些问题时,我可以重新编写代码.
何时应该LinkedList
使用,ArrayList
反之亦然?
是否存在可以存储在HashMap中的键条目数的理论限制,还是纯粹依赖于可用的堆内存?
另外,哪种数据结构最好存储大量对象(比如数十万个对象)?
我正在研究Java 8文档ArrayList
.我得到的最大数组大小定义为Integer.MAX_VALUE - 8
平均值2 ^ 31 - 8 = 2 147 483 639.然后,我的重点是,为什么8减去或why not less than 8
或more than 8
减去?
/**
* The maximum size of array to allocate.
* Some VMs reserve some header words in an array.
* Attempts to allocate larger arrays may result in
* OutOfMemoryError: Requested array size exceeds VM limit
*/
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
Run Code Online (Sandbox Code Playgroud)
我得到了一些相关的答案,但没有实现我的目标.
有些人根据文档给出了一些逻辑"Some …
我正在构建一个聊天应用程序.目前,我有所有的消息ArrayList
,让我思考 - ArrayList
设计要保留多少元素?100?1.000?10.000?
为什么不可能创建一个max int size的数组?
int i = 2147483647;
int[] array = new int[i];
Run Code Online (Sandbox Code Playgroud)
我找到了这个解释:
通过32位整数访问Java数组,最大理论数组大小为2147483647个元素.
但是你可以看到我的代码不起作用.创建一个大小的数组也是不可能的
new int[Integer.MAX_VALUE - 5];
Run Code Online (Sandbox Code Playgroud)
PS
为什么-5
呢?
除了链表之外,Java中是否有任何无界限列表?我必须将BLOB对象存储在列表中.我目前正在使用arrayList,但我担心当列表的大小增长时,arraylist可能无法存储(可能达到最大容量).我想过使用链表但看起来效率不高.
java ×6
arraylist ×3
arrays ×1
collections ×1
hashmap ×1
jvm ×1
jvm-hotspot ×1
linked-list ×1
performance ×1