Properties.propertyNames()以相反的顺序返回Enumeration - 为什么?

Tap*_*ose 4 java properties

在我的项目中,我正在使用一些属性文件.我注意到了Properties.propertyNames()的奇怪行为,它返回一个Enumeration,Enumeration的顺序相反.我做了一个测试:文件内容是:

TT.1=Development
TT.2=Application Setup / Release
TT.3=Project Management
TT.4=Meetings and Discussions
Run Code Online (Sandbox Code Playgroud)

代码是:

    Enumeration<?> enumeration = properties.propertyNames();
    while (enumeration.hasMoreElements()) {
        String key = (String) enumeration.nextElement();
        String value = properties.getProperty(key);
        System.out.println(key + " " + value);                        
    }
Run Code Online (Sandbox Code Playgroud)

输出是:

TT.4 Application Setup / Release
TT.3 Development
TT.2 Meetings and Discussions
TT.1 Project Management
Run Code Online (Sandbox Code Playgroud)

任何人都能说出背后的原因是什么?谢谢.
编辑:由于HashTable的键是TT.X形式,其中X是一个数字,我对它进行了排序,以便做出正确的顺序.这是下一个实现:

    this.taskTypeList = new ArrayList<String>(0); 
    Map<String, String> reverseTaskMap = new HashMap<String, String>(0);        
    Properties properties = loadTaskProperty();
    Enumeration<?> enumeration = properties.propertyNames();
    while (enumeration.hasMoreElements()) {
        String key = (String) enumeration.nextElement();
        String value = properties.getProperty(key);
        reverseTaskMap.put(key, value);            
    }

    LinkedList<Map.Entry<String, String>> linkedList = new LinkedList<Map.Entry<String, String>>(reverseTaskMap.entrySet());
    Collections.sort(linkedList, new Comparator<Map.Entry<String, String>>() {
        public int compare(Entry<String, String> object1, Entry<String, String> object2) {                            
            return Integer.valueOf(Integer.parseInt(object1.getKey().split("\\.")[1])).compareTo(Integer.valueOf(Integer.parseInt(object2.getKey().split("\\.")[1])));
        }
    });

    for (Iterator<Map.Entry<String, String>> iterator = linkedList.iterator(); iterator.hasNext(); ) {
        Map.Entry<String, String> entry = (Map.Entry<String, String>) iterator.next();
        taskTypeList.add(entry.getValue());
    }
Run Code Online (Sandbox Code Playgroud)

axt*_*avt 9

这是巧合.Properties不保证元素的任何特定顺序,因此顺序可以是任意的.

更具体地说,以下实现细节会导致此行为:

  • 由于您的密钥仅在最后一个字母中有所不同,因此它们的哈希码(由...生成String.hashCode())仅在最后几位中有所不同.

  • Properties是.的子类Hashtable.与之不同的是HashMap,HashTable不应用补充哈希函数来混合哈希码的位.由于Hashtable使用哈希码的最后几位作为多个哈希桶来放置元素,因此将元素放入随后的桶中.这是一个非常有趣的观点 - 它意味着这种实现Hashtable可以在一些真实场景中显示最差的情况,而HashMap不太可能.另一个原因青睐HashMapHashtable.

  • 出于某种原因HashtableEnumeration遍历以相反的顺序水桶,从而添加的元素以相反的顺序返回.


Ern*_*ill 5

这只是一个巧合; 这些属性实际上是以未定义的顺序返回的."属性"只是一个Hashtable; Hashtable枚举不会以任何特定顺序返回其键.


sta*_*ker 5

这种行为背后的原因是:

class Properties extends Hashtable<Object,Object>
Run Code Online (Sandbox Code Playgroud)

所以没有维持秩序,只是已经提到过的巧合.