This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading key/value pairs from a .properties file and creating objects

 
Vidya Moorthy
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34084
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vidya Moorthy
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34084
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic