JDK-6253413 : java.util.Properties should offer an Iterator for keys
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.util
  • Affected Version: 5.0
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2005-04-11
  • Updated: 2017-05-16
  • Resolved: 2005-09-17
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6
6 b53Fixed
Related Reports
Relates :  
Relates :  
Description
A DESCRIPTION OF THE REQUEST :
java.util.Properties should offer an Iterator for the keys. So far, only the keySet() from the superclass Hashtable can be used for this. But Hashtable has the generic type specification <Object, Object>. So you can't use the for-each loop

for (String key : p.keySet()) {
    ...
}

but only

for (Object key : p.keySet()) {
    String skey = (String) key;
   ...
}



JUSTIFICATION :
A proper Iterator would made it unnecessary to cast each key Object to String.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Calling keySet() on a Properties object should return Set<String> instead of the Properties superclass Set<Object>.

---------- BEGIN SOURCE ----------
import java.util.Properties;

public class Test {
    public static void main(String[] args) {
        //iterate through Properties:
        Properties p = System.getProperties();
        for (String key : p.keySet()) { //doesn't work
            System.out.println(key+": "+p.getProperty(key));
        }
    }
}

---------- END SOURCE ----------
###@###.### 2005-04-11 17:56:57 GMT

Comments
EVALUATION A new method is added in the Properties class: /** * Returns a set of keys in this property list that * the keys and their corresponding values are strings, * including distinct keys in the default property list if a key * of the same name has not already been found from the main * properties list. Any properties whose key or value is not * of type <tt>String</tt> are omitted. * * The returned set is not backed by the <tt>Properties</tt> object. * Changes to this <tt>Properties</tt> are not reflected in the set, * or vice versa. * * @return a set of keys in this property list that * the keys and their corresponding values are strings, * including the keys in the default property list. * @see java.util.Properties#defaults * @since 1.6 */ public Set<String> stringPropertyNames();
07-09-2005

SUGGESTED FIX Attached webrev.tar
07-09-2005