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()); }
source share