Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Problem wiht the load() method while using the Properties object  RSS feed

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi !
Well, I�m having a kind of side effect while using the load() Properties Object method .Appart from loading correctly all the parameters and values, it changes its order , i.e., it�s scrambling the original order (in terms of lines only ) and it affects seriously the aim of the program...
In fact, although this method needs and InputStream object associated , I�m using on of its subclasses (FileInputStream) ... But...I don�t think this might be the problem...
In fact, I�ve already created an enumeration with propertyNames , which worked very well, but my problem is that these data are in a different order regarding the file loaded. There�s no trace of sorting or some �logical scrambling�... (and I can�t change the parameters order in my application ...)
It�s interesting to note that up to 5 elements , the original order of the elements is kept... After that, the �auto-scrambling� begins...
Ex :
Consider the ListOfParameters.txt file as the source for the properties object:
MainBoilerstatus = off
SpareBoiler = on
MainValve = open
SecondaryValve = open
SpareValve = off
Container1 = presunto
Container2 = chester
Container3 = peru
After having loaded this list in a Properties object , if I list it with the list() method , I�ll have something like this :
SecondaryValve = open
Container3 = peru
SpareBoiler = on
Container2 = chester
MainBoilerstatus = off
Container1 = presunto
MainValve = open
SpareValve = off
Why the original order is changed when it�s loaded into a Properties object ???
So , please, how can I avoid this �side effect� ?
[ February 05, 2002: Message edited by: fabio gama ]
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't really - not with Properties. It extends Hashmap, and when it executes list() the order is largely determined by the order of the hash values. I suppose you could extend Properties yourself to override this behavior - you could maintain a List in a private instance field which contains the actual property values, and use the value in the Hashtable to store the index. Whenever a value is added or accessed, you'd need to use both Hashtable and List in concert - but the List would retain the order in which things were added.
If you decide to do this though, it's probably best to just wrtie a new class which contians a List and a Map, rather than actually extending Properties - since Properties should never have been built directly on Hashtable anyway. You can mess up a Properties by accessing the Hashtable methods directly. Rather than explain this in the documentation, they should hve simply made it impossible by excapsulating the Hashtable rather than extending it. Oh well... You can copy some of the source code from Properties to use in a new OrderedProperties class - like the load() method - and use the same method names that it uses, for simplicity. Just modify them to (a) keep track of the order in a List, and (b) hide the Hashtable/Map inside, as another private field. Enjoy...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!