Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I store an object on my hard drive?

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?

like image 931
Bradley Oesch Avatar asked Mar 11 '13 15:03

Bradley Oesch


2 Answers

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.

like image 100
Michał Ziober Avatar answered Oct 03 '22 21:10

Michał Ziober


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

like image 41
Ankur Shanbhag Avatar answered Oct 03 '22 20:10

Ankur Shanbhag