Blackberry:如何使数据结构(例如哈希表)可持久化?
我有这段运行良好的代码:
static {
myStore = PersistentStore.getPersistentObject(MY_LONG_KEY);
myList = (Vector) myStore.getContents();
if (_storedStations == null) {
myList = new Vector(4);
// prepopulate with fake data
myList.addElement(new MyItem("Eins"));
myList.addElement(new MyItem("Zwei"));
myList.addElement(new MyItem("Drei"));
myList.addElement(new MyItem("Vier"));
myStore.setContents(myList);
}
}
class MyItem implements Persistable {
.....
}
可能是因为 PersistentObject 文档说:
隐式持久化
请注意,某些类是隐式可持久化的:Boolean、Byte、Character、Integer、Long、Object、Short、String、Vector、 Hashtable。
另请注意,当您持久化一个对象时,它引用的任何可持久对象也将被持久化。
但是其他数据结构呢?例如 - 而不是上面使用的 Vector - 我想要一个 哈希表,其中键:字符串和值:MyItems。如何使其可持久化并确保 MyItem 内容真正被存储,无论该对象有多复杂?
为什么是 IntHashtable 列为 Persistable,但 Hashtable 不是?
谢谢你! 亚历克斯
I have this code which works well:
static {
myStore = PersistentStore.getPersistentObject(MY_LONG_KEY);
myList = (Vector) myStore.getContents();
if (_storedStations == null) {
myList = new Vector(4);
// prepopulate with fake data
myList.addElement(new MyItem("Eins"));
myList.addElement(new MyItem("Zwei"));
myList.addElement(new MyItem("Drei"));
myList.addElement(new MyItem("Vier"));
myStore.setContents(myList);
}
}
class MyItem implements Persistable {
.....
}
probably because the PersistentObject doc says:
Implicit persistance
Notice that some classes are implicitly persistable: Boolean, Byte, Character, Integer, Long, Object, Short, String, Vector, Hashtable.
Also note that, when you persist an object, any persistable object it refers to will also get persisted.
But what about other data structures? For example - instead of the Vector used above - I'd like to have a Hashtable with keys: Strings and values: MyItems. How can I make it Persistable and ensure that MyItem contents is really stored, no matter how complex that object is?
And why is IntHashtable listed as Persistable, but Hashtable is not?
Thank you!
Alex
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当你写关于隐式持久化的部分时,你自己列出了Hashtable,不知道你还想验证它是可持久的。无论如何,如果您希望 MyItems 是可持久的,只需将
public class MyItems Implements Persistable
作为其声明即可。它的工作方式是,如果您希望某些东西是可持久的,那么它保留的任何副本(即它的成员变量)也需要是可持久的。因此,如果 MyItems 需要存储字符串、整数向量和 MyItem,则需要确保 MyItem 也是可持久的。这会给您可以节省的内容带来一些限制。例如,如果 Persistable MyItems 的成员变量之一是 Bitmap,则您将无法创建 Persistable MyItems。另请注意,在方法内部使用非持久对象不会阻止您使类本身成为可持久的。
You listed Hashtable yourself when you wrote the part about Implicit Persistence, not sure what more you want to verify that it is Persistable. At any rate, if you want MyItems to be Persistable, just do
public class MyItems implements Persistable
as its declaration. The way it works is that if you want something to be Persistable, anything that it keeps a copy of (ie, it's member variables) needs to be Persistable as well. So if MyItems needs to store a String, a Vector of Integers, and a MyItem, you need to make sure MyItem is Persistable as well.This will give you some limitations on what you can save. For instance, you won't be able to create a Persistable MyItems if one of its member variables is a Bitmap. Also note that using a non-Persistable object inside of a method won't prevent you from being able to make the class itself Persistable.