使用 java.util.properties 存储 ArrayList 和 HashMap

发布于 2024-12-16 17:09:18 字数 126 浏览 5 评论 0原文

如何使用 java.util.properties 存储 ArrayList 和/或 HashMap 变量?如果不可能,我可以使用什么其他类来存储应用程序配置?

How can I store an ArrayList and/or a HashMap variable using java.util.properties? If it's not possible what other class can I use to store application configuration?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

傲性难收 2024-12-23 17:09:18

如果您只需要将集合序列化为字符串,我强烈推荐XStream。它使用反射将类序列化为 XML。如果默认行为不适用于您想要序列化的类,则有文档,但到目前为止,以下内容每次都对我有用:

XStream xstream = new XStream();
String xml = xstream.toXML(myObject);
MyClass deserializedObject = (MyClass)xstream.fromXML(xml);
assert deserializedObject.equals(myObject);

If you just need to serialize your collections into Strings, I highly recommend XStream. It uses reflection to serialize a class into XML. There is documentation if the default behavior doesn't work for the class you want to serialize, but the following has worked for me every time so far:

XStream xstream = new XStream();
String xml = xstream.toXML(myObject);
MyClass deserializedObject = (MyClass)xstream.fromXML(xml);
assert deserializedObject.equals(myObject);
沧桑㈠ 2024-12-23 17:09:18

所以...如果“不要这样做”对您不起作用,那么您需要以某种方式对数据进行编码。一种常见的技术是在每个元素的名称前面添加一些字符串。例如,如果我有一个包含 a->1、b->2、c->3 的映射 MyMap,我可能会存储在属性文件中:

MyMap.a=1
MyMap.b=2
MyMap.c=3

对于列表,您可以执行相同的操作,只需将索引映射到值。因此,如果 MyList 包含 {a,b,c}

MyList.0=a
MyList.1=b
MyList.2=c

这是一个 hack,其他人所说的都是真的。但有时你必须做你必须做的事。

So... if "don't do that" doesn't work for you, then you need to encode the data somehow. One common technique is to prepend some string to the name of each element. For example if I have a map MyMap containing a->1, b->2, c->3, I might store in the properties file:

MyMap.a=1
MyMap.b=2
MyMap.c=3

For lists, you can do the same, just mapping indices to values. So if MyList contains {a,b,c}

MyList.0=a
MyList.1=b
MyList.2=c

This is a hack, and everything everyone else said is true. But sometimes you gotta do what you gotta do.

月野兔 2024-12-23 17:09:18

Properties 基本上是 Map 意味着键和值都必须是 String 对象。如果你想要更高级的配置,你可以使用Spring。它是一个优秀的框架,我在每个项目中都使用它。 Spring 配置文件非常灵活。

Properties is basically Map<String, String> meaning both key and value must be String objects. If you want more advanced configuration, you could go with Spring. Its an excellent framework and I use it in every project. Spring config files are extremely flexible.

九厘米的零° 2024-12-23 17:09:18

java.util.Properties 仅适用于 String 键和值。它确实从 Hashtable 继承了 put() 和 putAll() 方法,但使用它们来“作弊”并不是一个好主意。您是否考虑过将配置信息存储在 HashMap 而不是 Properties 对象中?您必须稍微自定义序列化,但无论如何您都必须这样做,因为您无法利用 Properties 类的默认加载功能。

java.util.Properties is only intended to be used with String keys and values. It does inherit the put() and putAll() methods from Hashtable, but it's rarely a good idea to use those to "cheat". Have you considered just storing your configuration information in a HashMap rather than a Properties object? You would have to customize the serialization a bit, but you're going to have to do that in any case as you can't take advantage of the default loading functionality of the Properties class.

别再吹冷风 2024-12-23 17:09:18

存储 HashMap 很容易,因为 Map 中的每个键和值都可以由 Properties 对象中相应的键和值表示(请参阅 setProperty< 对于Properties 中的 /code> 方法,

您可以执行类似的操作,键是索引,值是相应索引中的项目

。请记住,属性文件仅存储字符串,因此您必须设计一种方法将对象中的键和值表示为字符串。

Storing a HashMap would be easy, since each key and value in the Map can be represented by a corresponding key and value in the Properties object (see the setProperty method in Properties.

For the ArrayList you could do something similar, the keys would be the indexes and the values the items in the corresponding indexes.

In both cases, remember that a properties file only stores strings, so you'd have to devise a way to represent the keys and values in your objects as strings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文