سؤال

I am writing a program that reads a huge amount of data from the hard disk and creates a hashmap from this data, then I do some processing that changes this initial hashmap a little. My problem, everytime I run my program, I create the same hashmap from the same data (lets call this the initial hashmap). Only the processing differs from one run to run. Is there anyway, I can create this hashmap once and save it persistently? Will serialization help?

هل كانت مفيدة؟

المحلول

It's a well known problem, there are many solutions ,among them I would suggest two: 1. save the hashmap in a sql database table with the primary key column containing the key values and another column containing the corresponding values; 2. use a nosql data store, there are many of them, among the others Redis or SimpleDb.

نصائح أخرى

Since HashMap is Serializable I'll use this way to save it:

public void saveStatus(Serializable object){
   try {
      FileOutputStream saveFile = new FileOutputStream("current.dat");
      ObjectOutputStream out = new ObjectOutputStream(saveFile);
      out.writeObject(object);
      out.close();
      fileOut.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
}

To recover your object just do it like this:

public Object loadStatus(){
   Object result = null;
   try {
      FileInputStream saveFile = new FileInputStream("current.dat");
      ObjectInputStream in = new ObjectInputStream(saveFile);
      result = in.readObject();
      in.close();
      fileIn.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return result;
}

And then you just can cast your object returned by this method to a HashMap and continue in the same place.

Similar questions have been asked here; I recommend Googling:

java persistent hash table

Having said that, you could manually save the key value pairs to a file or database, and reconstruct the hash on startup.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top