简体   繁体   English

在磁盘上存储Scala数据结构

[英]Storing Scala data structures on disk

I am trying to store result of some computations which I have stored in memory in HashMap and a List . 我正在尝试将一些计算的结果存储在HashMapList中的内存中。 Is there a way to store and load these data structures on disk so that I don't have to repeat this process over and over again? 有没有一种方法可以在磁盘上存储和加载这些数据结构,从而不必一遍又一遍地重复此过程?

EDIT 编辑

  1. I am looking for a standard library like pickle in python and not an external dependency. 我正在寻找像python pickle这样的标准库,而不是外部依赖项。

Both List and HashMap (as well as other collections) are Serializable in Scala, which means that standard Java serialization mechanisms can be applied to them. ListHashMap (以及其他集合)在Scala中均可Serializable ,这意味着可以将标准Java序列化机制应用于它们。

You can store the objects by creating an ObjectOutputStream and calling its writeObject(obj) method. 您可以通过创建ObjectOutputStream并调用其writeObject(obj)方法来存储对象。

The same applies for reading objects through an ObjectInputStream and its readObject() method. 通过ObjectInputStream及其readObject()方法读取对象也是如此。

The objects in the collections to read/write are also expected to be Serializable , so if it's not yet the case, you'll need to make them implement the interface. 集合中要读取/写入的对象也应该是Serializable ,因此,如果还不是这样,则需要使它们实现接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM