问题
I currently have a SparseStore matrix on which I perform a lot of counting and calculations. I want to store it to file, so I can later reuse it without re-doing all previous calculations.
I tried basic serialization in Java:
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(exportFileName));
outputStream.writeObject(mySparseStoreInstance);
but it seems the class doesn't implement java.io.Serializable:
java.io.NotSerializableException: org.ojalgo.matrix.store.SparseStore
I wouldn't mind changing to Array2D or some other ojalgo sparse structure, but also can't find if any of those can be serialized.
What is recommended to store sparse arrays to file?
回答1:
There is no built-in feature for this. At this point you have to implement something yourself. Extract the index-value pairs using nonzeros()
and write to a file, and then recreate using set(index, value)
one by one. Adding new elements in a strictly increasing index order is not terribly inefficient.
来源:https://stackoverflow.com/questions/57985654/how-to-store-ojalgo-sparse-array-to-file-in-java