| Author |
Reading key/value pairs from a .properties file and creating objects
|
Vidya Moorthy
Ranch Hand
Joined: Nov 13, 2003
Posts: 57
|
|
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. Thanks in advance Vidya
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26173
|
|
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.
|
[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
|
 |
Vidya Moorthy
Ranch Hand
Joined: Nov 13, 2003
Posts: 57
|
|
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[0] = 00 EmpId[1] = 01 EmpId[2] = 02 EmpFName[0] = Jane EmpFName[1] = John EmpFName[2] = Mary EmpLName[0] = Smith EmpLName[1] = Doe EmpLName[2] = Jane EmpAge[0] = 35 EmpAge[1] = 28 EmpAge[2] = 42 and the keys are read as [EmpLName[2]=Jane, EmpFName[2]=Mary, EmpAge[1]=28, EmpId[1]=01, EmpLName[1]=Doe, EmpFName[1]=John, EmpAge[0]=35, EmpId[0]=00, EmpLName[0]=Smith, EmpFName[0]=Smith, EmpAge[2]=42, EmpId[2]=02] Thanks Vidya
|
 |
Guido Sautter
Ranch Hand
Joined: Dec 22, 2004
Posts: 142
|
|
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[0] 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 ]
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26173
|
|
Originally posted by Vidya Moorthy: [EmpLName[2]=Jane, EmpFName[2]=Mary, EmpAge[1]=28, EmpId[1]=01, EmpLName[1]=Doe, EmpFName[1]=John, EmpAge[0]=35, EmpId[0]=00, EmpLName[0]=Smith, EmpFName[0]=Smith, EmpAge[2]=42, EmpId[2]=02]
If you sorted alphabetically by key name, you would get: EmpAge[0] EmpAge[1] EmpAge[2] EmpFName[0] EmpFName[1] EmpFName[2] EmpId[0] EmpId[1] EmpAge[2] EmpLName[0] EmpLName[1] EmpLName[2] How is this different from what you want?
|
 |
 |
|
|
subject: Reading key/value pairs from a .properties file and creating objects
|
|
|