Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

for-each and Properties Class

 
Dennis Putnam
Ranch Hand
Posts: 313
  • Mark post as helpful
  • send pies
  • 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.
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
 
Dennis Putnam
Ranch Hand
Posts: 313
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant keySet. Typo. However, I figured it out.
 
Mike Simmons
Ranch Hand
Posts: 3080
14
  • Likes 3
  • Mark post as helpful
  • send pies
  • 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:
 
Rob Spoor
Sheriff
Pie
Posts: 20533
54
Chrome Eclipse IDE Java Windows
  • Likes 3
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 3080
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic