How to Create a Disk Based List in Java

Occasionally when programming in Java you will need a List too large to hold in memory. MapDB is an open source Java library that allows you to create a disk based List (and other Java Collections) very easily.

MapDB is a hybrid of an embedded database engine and the Java Collections framework. It provides Maps, Sets, Lists, Queues, etc. that can be stored in off-heap memory or on disk. MapDB is available via Maven central so it can be easily included in your project. Visit the MapDB home page for more information.

Creating a Disk Based List in Java

Below is an example of creating a disk based ArrayList (IndexTreeList is an ArrayList type object backed by a tree). You can see in the example that DB and DBMaker objects are used to create the List. Each instance of DB represents a single transaction session, so commit() is needed to actually save the data to disk. Note that multiple Lists, Sets, etc. can be written to the same file.

Reading a Disk Based List in Java

Reading a List that has been previously serialized to disk is very similar to creating a List for writes. We simply have to specify file containing the List, as well as the name of the List to deserialize. Also, there is no need to commit() since we are just reading the data.

MapDB Maven Dependency

Sample MapDB Maven dependency in pom.xml:

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">