Tardis Xu Tardis Xu - 2 years ago 117
Java Question

Converting java.util.Properties To HashMap<string,string>

Properties properties = new Properties();
Map<String, String> map = new HashMap<String, String>(properties);// why wrong?

java.util.Properties is a implement of Map, And HashMap constructor receive a Map type param.
But why must convert explicitly?

fge fge
Answer Source

This is because Properties extends Hashtable<Object, Object> (which, in turn, implements Map<Object, Object>). You attempt to feed that into a Map<String, String>. It is therefore incompatible.

You need to feed string properties one by one into your map...

For instance:

for (final String name: properties.stringPropertyNames())
    map.put(name, properties.getProperty(name));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download