Why Java Enumeration doesn't return properties list in order?

后端 未结 4 1064
情话喂你
情话喂你 2020-12-19 20:48

I\'m creating a simple program with the possibility to change the language and I would convert the list of properties in the file myBundle.properties in a String array.

相关标签:
4条回答
  • 2020-12-19 21:23

    Most likely you are getting back a PropertyResourceBundle, which internally uses a HashMap for storage. HashMaps are not ordered.

    This is because ResourceBundles should be used for retrieving value by key, not for enumerating in sorted order based on the original property file.

    0 讨论(0)
  • 2020-12-19 21:24

    There are two places where I would not assume the order.
    1.From the Vector and 2.labels.getKeys();

    Try it on a enum that is not instantiated by a collection.

    0 讨论(0)
  • 2020-12-19 21:44

    Se if replacing

    Enumeration<String> keys = labels.getKeys();
    

    with

    List<String> keys =  Collections.list( labels.getKeys() );
    Collections.sort(keys);
    

    helps

    0 讨论(0)
  • 2020-12-19 21:47

    I do not know the exact details of the ResourceBundle class, but when looking at your code example, it seems that it has key/value pairs.

    This suggests that it stores its content in an HashMap. (Again, as I do not know ResounrceBundle, this is a hunch) HashMap keys (and values) are unordered as they are stored on such a way that the value can be easily found for a given key.

    0 讨论(0)
提交回复
热议问题