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
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.
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);
}
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()
Another way of saving stuff which has the advantage of being language and transport neutral is to use Google's Protocol Buffers.
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.
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/