This question already has an answer here:
I need to read a properties file and generate a Properties class in Java. I do so by using:
Properties props = new Properties();
props.load(new FileInputStream(args[0]));
for (Enumeration e = props.propertyNames(); e.hasMoreElements();) {
}
However, the properties returned by props.propertyName is not in the order of the original properties file. I understand that Properties are just old fashioned, non-generified Hashtables. I'm looking for a work around. Any idea? Thank you!
Similar to one of the above, but w/out the overhead of maintaining our own list of values. All we have to do is maintain a separate ordered list of the keys, and provide a new "keys()" method.
To solve the problem: "to execute classes based on the order in the properties file." I normally used one of 2 possibilities:
1 - use one property as a comma-separated list with the class-names or with the keys to the class definition
or (useful if the "definition" consists of more than one property)
2 - use a key followed by an index (counter). Read the keys in a loop until no value is found.
You can extend Properties and delegate all map methods to a LinkedHashMap to retain the order. Here is an example (you may need to override some more methods):
The fact they are represented as a
Hashtable
under the hood means that their order is not kept in any fashion.I'd suggest you "roll your own" properties reader if you're absolutely desperate for this functionality.
Proper implementation of keySet:
You may want to implement your own Properties class with similar functionalities. It will not be possible for you to obtain the order since, as you already pointed out, it uses
Hashtable
.