This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Hi all I am trying to read from a .properties file which has key value pairs for Employee's id, first name, last name and age. I tried to use the Property class's .keys()method into an iterator and construct Employee objects. But the keys were stored in a different order than the one which I stored. I have used the keySet() and store the keys in a TreeSet which will keep it sorted. Is there any other way to construct Employee objects and store it in an ArrayList and use Collections.sort(L). This is the code which is using the TreeSet. I would appreciate ideas for the other way.
Vidya, I'm not clear on what the magic number of "3" means in your code, but here's my two cents.
The easiest thing I can think of is to wrap properties.keySet() in an ArrayList as: new ArrayList(properties.keySet());
Since an ArrayList has an order, this allows you to call Collections.sort(). You would need to have Employee implement the comparable interface so it knows you want to sort by the key. Alternatively you could implement a separate Comparator for this.
Thanks for the suggestion. But the problem I have with that is my keySet pulls the properties in a different order than I put and it becomes difficult for me to set the Employee attributes. 3 is the number of Employee objects I have in the property file. Here is my property file # Employee Object's ID, first name, last name, age EmpId = 00 EmpId = 01 EmpId = 02 EmpFName = Jane EmpFName = John EmpFName = Mary EmpLName = Smith EmpLName = Doe EmpLName = Jane EmpAge = 35 EmpAge = 28 EmpAge = 42
and the keys are read as [EmpLName=Jane, EmpFName=Mary, EmpAge=28, EmpId=01, EmpLName=Doe, EmpFName=John, EmpAge=35, EmpId=00, EmpLName=Smith, EmpFName=Smith, EmpAge=42, EmpId=02]
Maybe using a Properties for this porpose is not the best way to go ... If you have a chance to influence the format of the data file you are reading, CSV might be worth giving a try. Otherwise, I'd try to "turn the keys around", converting EmpID to 0.EmpID, which will make sure you got all the data of each of the employees en block. In this way, you can check if the data of a new employee has started just by looking at the part of the key before the dot. This will also make your code easier to maintain if the number of attributes per employee changes. [ May 05, 2008: Message edited by: Guido Sautter ]
author & internet detective