• 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

for-each and Properties Class

 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to process all the keys/values in a Properties class. I cannot seem to figure out the syntax of the for loop. I want the correct syntax to do something like:

I guess I need to somehow convert the list of keys to an array or set? TIA.
 
Ranch Hand
Posts: 859
IBM DB2 Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no "keys"

http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html#propertyNames()

WP
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I meant keySet. Typo. However, I figured it out.
 
Master Rancher
Posts: 4806
72
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunately, the Properties class was made to implement Map<Object, Object> when it clearly should have been Map<String, String>. This was probably because Properties was written long before generics came into the language, and Sun in their infinite wisdom chose to retroactively implement Map<Object, Object> in order to not break backwards compatibility for clients using their Properties in an incredibly stupid way. That is, Properties has always allowed people to put things other than Strings in as either keys or values, if they use put() rather than setProperty(), even though only an insane person would ever do this. Rather than fix this error, Sun chose to continue it. That's why you can't simply use "String key : props.keySet()" as you would expect.

Here's another alternative:
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not loop over the entry set?
Unfortunately you'll need those casts since somebody thought that Properties should extend Hashtable<Object, Object> instead of Hashtable<String, String>, probably because someone somewhere abused Properties and put something other than Strings in it. It's a rubbish reason. If someone was trying to abuse Properties like that they deserve a compiler error.

Wow, beaten by 3 seconds with an answer that's more or less what I said. Auch. Now I know how Campbell feels all the time
 
Mike Simmons
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic