Properties.propertyNames () returns Enumeration in reverse order - why?

In my project, I use some properties file. I noticed the strange behavior of Properties.propertyNames (), it returns Enumeration that the enumeration is in reverse order. I did a test: File Content:

TT.1=Development TT.2=Application Setup / Release TT.3=Project Management TT.4=Meetings and Discussions 

Code:

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

Output:

 TT.4 Application Setup / Release TT.3 Development TT.2 Meetings and Discussions TT.1 Project Management 

Can anyone say what is the reason? Thanks.
Edit: Since the hash table key is of the form TT.X, where X is the number, I sorted it to make the correct order. Here is the following implementation:

  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()); } 
+4
source share
3 answers

That's a coincidence. Properties does not guarantee any particular order of elements, so the order can be arbitrary.

More specifically, the following implementation data leads to this behavior:

  • Since your keys differ only in the last letter, their hash codes (created by String.hashCode() ) differ only in the last few bits.

  • Properties is a subclass of Hashtable . Unlike HashMap , Hashtable does not use an additional hash function to mix bits of the hash code. Since the Hashtable uses the last bit of hashcode as the number of hash bucket for placing items, your items are placed in subsequent buckets. This is a really interesting point - this means that this Hashtable implementation may seem to be worse performance in some real-world scenarios, while for HashMap this is unlikely. Another reason to support a HashMap over a Hashtable .

  • For some reason, the Hashtable Enumeration moves the buckets in the reverse order, so added items are returned in the reverse order.

+9
source

It is just a coincidence; properties are actually returned in undefined order. "Properties" is just a Hashtable; Hashtable enumerations do not return their keys in any particular order.

+5
source

The reason for this is:

 class Properties extends Hashtable<Object,Object> 

Thus, the order is not supported, just a coincidence, as already mentioned.

+5
source

Source: https://habr.com/ru/post/1346895/


All Articles