我正在使用枚举来读取属性文件。假设我的属性文件中的值为 lon2qaidxiat01.idx.local, master, 2015-02-13, 2015-02-28
。但是当我尝试使用 Enumeration
读取相同内容时,它会以随机顺序读取,例如 2015-02-13,master,2015-02-28,lon2qaidxiat01.idx.local
.
下面是我的代码:
try {
dbProperties.load(new FileInputStream("config/db.properties"));
Enumeration enuKeys = dbProperties.elements();
while (enuKeys.hasMoreElements()) {
String key = (String) enuKeys.nextElement();
String value = dbProperties.getProperty(key);
System.out.println(key);
paramList.add(key);
}
...
请建议如何使用枚举按顺序读取。
最佳答案
这是因为Properties
只是一个 Hashtable
并且不保证任何特定的顺序。 (没有办法“解决”这个问题。调用 dbProperties.load
时顺序丢失。)
如果顺序很重要,请使用 LinkedHashMap
或 List
键/值对(并滚动您自己的加载例程)。
关于java - 枚举中的顺序不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28938495/