I'm looking for a data structure same 开发者_运维知识库as ArrayList in Java that saves items on disk instead of memory. Does java have such a data structure? Thanks
I want to have a dynamic structure that saves items on memory and when its size exceeds some value, save new items on disk until the size goes below the value.
You can yourself do it: Serialize the ArrayList
to disk.
Make sure that the contents of the list are serializable, i.e., the objects in the list should implement the Serializable
interface.
then
to write to file:
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName));
oos.writeObject(list);
oos.flush();
oos.close();
To read from file:
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName));
List<YourClass> list = ois.readObject();
ois.close()
If you need to change data in this ArrayList often, changing disk image, then why not consider something like http://www.hibernate.org/ ? Well, it is much more complicated than ArrayList but also gives more featues.
Just to make the set of answers complete :)
XStream
XStream is a simple library to serialize objects to XML and back again.
Person joe = new Person("Joe", "Walnes");
joe.setPhone(new PhoneNumber(123, "1234-456"));
joe.setFax(new PhoneNumber(123, "9999-999"));
Now, to convert it to XML, all you have to do is make a simple call to XStream:
String xml = xstream.toXML(joe);
The resulting XML looks like this:
<person>
<firstname>Joe</firstname>
<lastname>Walnes</lastname>
<phone>
<code>123</code>
<number>1234-456</number>
</phone>
<fax>
<code>123</code>
<number>9999-999</number>
</fax>
</person>
Check out http://jakarta.apache.org/jcs/. This provides a way to manage objects on disk and ram. Another possible solution may to use http://code.google.com/p/pcollections/
MapDB (mapdb.org) is a library that persists java objects to disk in various collections: Sets, Maps, Queues.
It supports caching so you can have your most frequent items in memory.
Another way of saving stuff which has the advantage of being language and transport neutral is to use Google's Protocol Buffers.
see https://dzone.com/articles/a-filebasedcollection-in-java-for-big-collections
try(FileBasedCollection<Integer> fbc = new FileBasedCollection<>()) {
for(int i = 1; i < 1000_000; i++) {
fbc.add(i);
}
long sum = 0;
try(FileBasedIterator<Integer> iter = fbc.iterator()) {
while(iter.hasNext()) {
Integer i = iter.next();
sum += i;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("sum: " + sum);
}
精彩评论