7

I'm working on a project that uses a couple maps that can have over 100,000 keys. Currently I'm creating the maps at each runtime using an abbreviated form of the data to save time so that the maps only have around 1,000 keys. But I would like to test with the full extent of data that I have available.

I'm wondering how I can create a map in a class, then save that object to the hard drive, then be able to reference that object in different classes so that I do not need to create and fill the map each time I test the class. I'm writing the class in Java using Eclipse.

Edit: I believe it is called Object Serialization, but would I have to read the whole map before use if it's serialized? Or would it effectively be the same as calling a local variable?

Bradley Oesch
  • 763
  • 10
  • 22
  • 3
    Is this of relevance: http://stackoverflow.com/questions/1536953/recommend-a-fast-scalable-persistent-map-java ? – NPE Mar 11 '13 at 15:09
  • Sure, you can create a file. However, you haven't told us anything about what the keys or values are, which makes it hard to suggest an approach. – Jon Skeet Mar 11 '13 at 15:09
  • 1
    there are two common ways: via database or file based. – nano_nano Mar 11 '13 at 15:09
  • 3
    Are you talking about persisting an object to the hard drive, so that you can store it between executions of your program and reload it when you run later? Or are you trying to avoid having the map in memory **at all**, such that it *only* exists on the hard drive, as some sort of memory-saving measure? What is it you're trying to accomplish here? – Andrzej Doyle Mar 11 '13 at 15:10
  • I'd like to have an object stored on the hard drive that I can refer to like a variable, or like how one refers to a .txt file to read. The point is to have an object already created so I don't have to create it at each runtime. – Bradley Oesch Mar 11 '13 at 15:27
  • Use `ObjectOutputStream`. – shuangwhywhy Mar 11 '13 at 15:31

3 Answers3

1

I had similar problem. First I used HSQLDB. After that I checked EHCache. And it makes the difference - works faster and is easier to understand.

You also can look on NOSQL page, paragraph "Key Value / Tuple Store". For sure you will find something for you.

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
1

If your map contains objects which are serializable then you can directly write map to the disk using serailization. Eg: Map of String,Integer

But if your map holds any custom class that you have created then your class must be made serializable by implementing Serializable interface.

You can explicitly decide the way your class is written to disk using serialization by overriding writeObject(ObjectOutputStream s) for writing and readObject(ObjectInputStream ois) for reading back the contents of the class.

These methods i.e. writeObject() and readObject() will be called implicitly when serialization and deserialization takes place. Be careful while implementing these methods as you must you must read the elements back in the same order as writing.

Sample example how to implement these methods : Sample example

Ankur Shanbhag
  • 7,746
  • 2
  • 28
  • 38
0

I faced a similar issue some time ago. I used Berkeley DB, it fitted perfect. It is a good alternative if you ask me: http://docs.oracle.com/cd/E17277_02/html/GettingStartedGuide/index.html

Marcelo Tataje
  • 3,849
  • 1
  • 26
  • 51