调整集合以容纳大量对象
如果一个集合(如数组列表)将存储数千个自定义对象(例如具有多个属性的 Person),那么我的代码或集合的构造函数中是否需要执行任何操作来为如此大的集合做好准备。
我并没有真正考虑专用线程等,而是更多地考虑负载因子(对于上述场景,我是否需要触及这个?)。
谢谢
If a collection, like an arraylist, will be storing custom objects (eg Person with several properties) in the thousands, is there anything to do in my code or in the constructor of the collection to prepare it for such a large collection.
I'm not really thinking of dedicated threads etc, but more along the lines of the load factor (do I need to touch this for the above scenario?).
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一种不同的方法:
由于我们正在谈论如此庞大的集合,因此它会“吃掉”您的 RAM,
我认为您应该考虑将此集合存储在数据库中,并且仅在必要时才读/写/更新。
A different approach:
Since we are talking about such a Huge Collection, that would "Eat up" you RAM,
I think you should consider storing this collection in a database and read/write/update ONLY when you must.
您可以执行以下操作:
预先分配具有指定大小(例如 10000)的数组,以便在添加元素时不必重新分配。除此之外,你无能为力。另外,对于 ArrayList 存储的引用类型并不重要,因此这些信息不能真正帮助您进行优化。
You can do:
which pre-allocates the array with the specified size (e.g 10000) so that it doesn't have to re-allocate as you add elements. Apart from that, there is nothing you can do. Also - it doesn't matter to the ArrayList what kind of reference it is storing, so that information can't really help you in optimisation.
我只是将集合初始化为接近最终大小的大小,以最大程度地减少调整大小的次数:
I'd just initialize the collection to a size that would be close to the final size, in order to minimize the number of resizings: