• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Preserve order for values from Properties file

 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A TreeMap wouldn't preserve the order, it would reorder the keys alphanumerically.

A LinkedHashMap would preserve order.
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I got it with using StreamTokenizer and linkedhashmap..

Thanks once again!!!
reply
    Bookmark Topic Watch Topic
  • New Topic