How to Create a Disk Based Map in Java

Occasionally when programming in Java you will need a Map too large to hold in memory. MapDB is an open source Java library that allows you to create a disk based Map (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 Map in Java

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

Reading a Disk Based Map in Java

Reading a Map that has been previously serialized to disk is very similar to creating a Map for writes. We simply have to specify file containing the Map, as well as the name of the Map 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="">