• Post Reply Bookmark Topic Watch Topic
  • New Topic

Preserve order for values from Properties file  RSS feed

 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have to read a file from a properties file and create a property object..
but i want the order of the values in the file to be preserved in the properties object.

The problem is that i have a third party jar which reads the file and gives me a properties file..but it doesnt gives in the order...the values were entered.

Thanks in advance,
Regards
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm pretty sure that thats not possible since the Properties class extends Hashtable (not the best design decision) and the Iterator over a Hashtable key set/entry set makes no guarantees as to the ordering of its elements. Why do you need preserve the ordering of the .properties file?
 
Naveen K Garg
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kumar,

Since Properties are Hashtables, they scramble the order of the elements. If you want to preserve order, and don't need key lookup, you can parse the file yourself with a StreamTokenizer and put it in an array.

The other alternative is to use the TreeMap for getting the ordered set of values.

Regards
Naveen
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed convert Hashtable to a TreeMap.

But, using the order as it is in the propertiesfile sounds like bad practice. Why do you want to do it anyway? Maybe there are other and better solutions.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A TreeMap wouldn't preserve the order, it would reorder the keys alphanumerically.

A LinkedHashMap would preserve order.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Naveen K Garg:
Hi Kumar,

Since Properties are Hashtables, they scramble the order of the elements. If you want to preserve order, and don't need key lookup, you can parse the file yourself with a StreamTokenizer and put it in an array.

The other alternative is to use the TreeMap for getting the ordered set of values.

Regards
Naveen


Or, instead of writing your own parser, you could get the code for the Properties class, rename/repackage it, and modify it to use a List or whatever implementation that you need. This has the benefit of having the same parsing functionality as Properties (although if Sun updates Properties' parsing in the future, your code won't update, of course).

I've had to do this, and I called the resulting class a Script, since that was the use to which I was putting it (a simple script, where keys and values had meaning, and had to happen in order).
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The order has to be preserved and according to that the values have to be sent back..

because if the order is not preserved...the conditions would not be true in the program..

Regards
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
A TreeMap wouldn't preserve the order, it would reorder the keys alphanumerically.

A LinkedHashMap would preserve order.


Seems that I misunderstood exact meaning of "preserve". Thank you.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I got it with using StreamTokenizer and linkedhashmap..

Thanks once again!!!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!