如何在java中对属性进行排序? [英] How to sort Properties in java?
问题描述
我有一个Properties变量,有时我需要添加其他属性。
I have a Properties variable and sometimes I need to add other Properties to it.
Properties myBasicProps = this.getClass.getResourceAsStream(MY_PROPS_PATH);
...
Properties otherProps = new Properties();
otherProps.load(new StringReader(tempPropsString)); //tempPropsString contains my temporary properties
myBasicProps.putAll(otherProps);
我想在此之后排序 myBasicProps
。我不想获取所有键和值,使用 Collections.sort()
对它们进行排序,然后将其全部放入新对象。还有更好的方法吗?
I want to sort myBasicProps
after this. I don't want to get all keys and values, sort them with Collections.sort()
and then put it all to a new object. Is there a better way?
推荐答案
不, java.util.Properties
extends java.util.Hashtable
,它没有为键或值定义可预测的排序顺序。
No, java.util.Properties
extends java.util.Hashtable
which doesn't define a predictable sort order for keys or values.
你可以尝试将所有值转储为类似 java.util.TreeMap
的内容,这会对您的密钥进行自然排序。
You could try dumping all values into something like java.util.TreeMap
, which will impose a natural ordering on your keys.
这篇关于如何在java中对属性进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!