Newbie: iterating system properties

R

roger_pearse

I'm sure this is a silly question, but I'd like to iterate through the
system properties, and can't figure out from the Sun tutorial how to do
this. Modifying the simplest HelloWorldApp:

---start---
import java.util.*;

class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); //Display the string.

Properties myprops = System.getProperties();
// need to loop around these somehow and print out...
for (Iterator i = myprops.iterator(); myprops.hasNext(); ) {
}
}
}
---end---

This won't compile:

HelloWorldApp.java:13: cannot find symbol
symbol : method iterator()
location: class java.util.Properties
for (Iterator i = myprops.iterator();
myprops.hasNext(); ) {
^
HelloWorldApp.java:13: cannot find symbol
symbol : method hasNext()
location: class java.util.Properties
for (Iterator i = myprops.iterator();
myprops.hasNext(); ) {
^
2 errors

No doubt I am doing something really crass -- anyone care to help me?

Thanks,

Roger Pearse
 
T

Thomas Fritsch

I'm sure this is a silly question, but I'd like to iterate through the
system properties, and can't figure out from the Sun tutorial how to do
this. Modifying the simplest HelloWorldApp:

---start---
import java.util.*;

class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); //Display the string.

Properties myprops = System.getProperties();
// need to loop around these somehow and print out...
for (Iterator i = myprops.iterator(); myprops.hasNext(); ) {
}
}
}
---end---
Unfortunately Properties doesn't implement the Iterable interface and
hence doesn't have the iterator() method.
Therefore you must do it the old-fashioned way, i.e. using Enumeration:

for (Enumeration e = myprops.keys(); e.hasMoreElements(); /**/) {
String key = (String) e.nextElement();
String value = myprops.getProperty(key);
System.out.println(key + " = " + value);
}
 
R

roger_pearse

Thomas said:
I'm sure this is a silly question, but I'd like to iterate through the
system properties, and can't figure out from the Sun tutorial how to do
this. Modifying the simplest HelloWorldApp:

---start---
import java.util.*;

class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); //Display the string.

Properties myprops = System.getProperties();
// need to loop around these somehow and print out...
for (Iterator i = myprops.iterator(); myprops.hasNext(); ) {
}
}
}
---end---
Unfortunately Properties doesn't implement the Iterable interface and
hence doesn't have the iterator() method.
Therefore you must do it the old-fashioned way, i.e. using Enumeration:

for (Enumeration e = myprops.keys(); e.hasMoreElements(); /**/) {
String key = (String) e.nextElement();
String value = myprops.getProperty(key);
System.out.println(key + " = " + value);
}

I am very grateful for the explanation, and the example. I shall now
go and find out about Enumeration.

All the best,

Roger Pearse
 
T

Thomas Hawtin

Thomas said:
Unfortunately Properties doesn't implement the Iterable interface and
hence doesn't have the iterator() method.
Therefore you must do it the old-fashioned way, i.e. using Enumeration:

You can do it the new (1.2) way. Properties implements Map. So you can
do something like:

for (Map.Entry<Object, Object> entry : properties.entrySet()) {

Or if you just wanted keys:

for (Object key : properties.keySet()) {

Or from 1.6:

for (String key : properties.stringPropertyNames()) {

(The last two can produce a different set.)

Tom Hawtin
 
R

roger_pearse

Thomas said:
Unfortunately Properties doesn't implement the Iterable interface and
hence doesn't have the iterator() method.
Therefore you must do it the old-fashioned way, i.e. using Enumeration:

I now have a further question, which again is probably daft, but
followed on when I tried to find out how I might have known this
information without appealing for it.

I went and searched for Enumeration, which I found to be an interface:

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Enumeration.html

I saw the stuff about using the elements() method of the classes to get
an Enumeration object. I remembered that Properties was a subclass of
Hashtable, and so it was.

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Hashtable.html
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Properties.html

But when I looked at Properties, the interfaces implemented were only
Cloneable, Map and Serializable. The same was true of Hashtable. So
how should I have known that it implemented (or inherited an
implementation of) Enumeration?

I hope this is clear -- I'm trying to find how I should have got the
answer from the docs myself, for next time.

All the best,

Roger Pearse
 
C

christian.bongiorno

Hi there. If it's a Map it has a key, a value and an entry -- all
three you can iterate on

Map sortedProps = new TreeMap(System.getProperties());

for(Iterator i = sortedProps.entrySet().iterator(); i.hasNext(); )
System.out.println(i.next());

This will actually sort the properties for you if you are looking for
one. I routinely use this in the debugger to look at a property where I


1) can't be bothered to type out the entry property name and fetch it,
or
2) can't remember the entire name/spell it correctly

Since you seem new enough, System.out.println calls the "toString()" on
any object passed in (null objects just print "null") -- so, rather
than even having to do the String formatting for the output you will
get {key,value} or {key => value}. You could take it one step further
and just do System.out.println(System.getProperties()) and you would
get the AbstractMap implementation of toString()



Either way, it makes your life simpler

Christian
http://christian.bongiorno.org/
 
R

Roedy Green

But when I looked at Properties, the interfaces implemented were only
Cloneable, Map and Serializable. The same was true of Hashtable. So
how should I have known that it implemented (or inherited an
implementation of) Enumeration?

With Iterators the collection implements Iterable which means the
Collection will spew out an Iterator on demand.

With Enumerators there in no equivalent to Iterable. You just have to
dig around for the method that will give you the Enumeration.

Dig around and you will find Properties.elements which produces an
Enumeration.

For details see http://mindprod.com/jgloss/enumeration.html
and http://mindprod.com/jgloss/iterator.html

This brings to a peeve. for:each should support Iterators, not just
Iterables.
 
C

Chris Uppal

I hope this is clear -- I'm trying to find how I should have got the
answer from the docs myself, for next time.

Your difficulty is partly a symptom of bad design in Properties and (arguably)
Hashtable. The information you were looking for is not in the usual places.

The only way that you could have found it is by the following algorithm.

(0) Start with the class you are interested in.

(1) Read the /whole/ of it's JavaDoc (class and method comments) to see if
there's anything which would solve your problem. If you find anything then the
search terminates.

(2) Switch your attention to the immediate superclass.

(3) GOTO 1.

If you end up in Object with still no solution, then repeat the search but this
time branch outwards to check package JavaDoc, and the JavaDoc of any
implemented interfaces too.

Also check the listed subclasses of the class you started with. It's possible
that you have been given an object which is actually an instance of a
documented subclass of the declared class. It might be that the feature you
are looking for is only available on the subclass, and that you are expected to
use a cast to get access to the feature (HttpURLConnection, anyone ?).

I.e, not just RTFM, but RT/W/FM ;-)

-- chris
 
R

roger_pearse

Chris said:
Your difficulty is partly a symptom of bad design in Properties and (arguably)
Hashtable. The information you were looking for is not in the usual places.

The only way that you could have found it is by the following algorithm.

(0) Start with the class you are interested in.

(1) Read the /whole/ of it's JavaDoc (class and method comments) to see if
there's anything which would solve your problem. If you find anything then the
search terminates.

(2) Switch your attention to the immediate superclass.

(3) GOTO 1.

If you end up in Object with still no solution, then repeat the search but this
time branch outwards to check package JavaDoc, and the JavaDoc of any
implemented interfaces too.

Also check the listed subclasses of the class you started with. It's possible
that you have been given an object which is actually an instance of a
documented subclass of the declared class. It might be that the feature you
are looking for is only available on the subclass, and that you are expected to
use a cast to get access to the feature (HttpURLConnection, anyone ?).

I.e, not just RTFM, but RT/W/FM ;-)

Well, I thought I did. But it didn't indicate that Properties
implemented Enumeration. Did I miss something? (This is a genuine
question).

All the best,

Roger Pearse
 
T

Thomas Fritsch

Chris Uppal wrote: [...]
I.e, not just RTFM, but RT/W/FM ;-)

Well, I thought I did. But it didn't indicate that Properties
implemented Enumeration. Did I miss something? (This is a genuine
question).
Yes, Properties does not implement the Enumeration interface.
But it has a method which returns an Enumeration.
public Enumeration keys();

Therefore, you have to browse through the API docs
not only looking for "... implements Enumeration",
but also looking for methods declared as "public Enumeration ****()"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top