如何有效地将 TreeSet 的一部分保存到文件中?并重新加载它? (Java问题)
我正在使用 TreeSet 来存储一些信息,以便根据某种顺序对其进行排序。
当 TreeSet 变得非常大(> 1GB)时,我想将 TreeSet 中最小的元素保存到文件中,以释放一些 RAM。然后,当有更多可用 RAM 时,我希望能够将这些元素重新加载到内存中来处理它们。
我的问题是:是否有某种有效的方法将 TreeSet 的一部分存储到文件中并稍后将它们恢复到内存中?
请注意,当我将元素重新加载到内存中时,它可能是新 TreeSet 的一部分,也可能是同一个 TreeSet 中。
感谢您提供有关如何执行此操作的任何想法!
I'm working with a TreeSet to store some information, so that it is sorted according to some order.
When the TreeSet becomes very large (>1GB), I want to save the smallest elements in the TreeSet to a file, to free some RAM. Then later, when there is more free RAM, I want to be able to reload these elements into memory to process them.
My question is: is there some efficient way of storing part of a TreeSet to file and restoring them into memory later?
Note that when I reload the elements into memory, it could be part of a new TreeSet or into the same TreeSet.
Thanks for any idea about how to do this!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你用 TreeSet 做什么?内容经常变化吗?您是否想在速度或磁盘使用方面提高效率?
与内存相比,读取和写入文件的速度非常慢,如果文件和内存版本经常更改,则保持文件和内存版本同步可能会很困难。
也许使用数据库是有意义的。有几种轻量级数据库(例如 derby 和 sqllite)可以嵌入到您的应用程序中。数据库的设计目的是考虑内存与文件问题,如果您有大于 1Gb 的数据,也许组织它是有意义的。
What are you using the TreeSet for? Do the contents change often? Are you trying to be efficient in terms of speed, or disk usage?
Reading and writing to a File is very slow compared to memory, and keeping the file and memory versions in sync might be challenging if they change often.
Maybe it makes sense to use a database. There are several lightweight databases such as derby and sqllite which can be embedded in your application. Databases are designed to worry about the memory vs. file issue, and if you have >1Gb of data, maybe it makes sense to organise it.