0

I have the Map of type Object, i need to convert this map into the type String.

Map<String, String> map = new HashMap<String, String>();    
Properties properties = new Properties();    
properties.load(instream);     

Can any body please tell me, how to assign the properties to above map?

Thanks & Regards, Msnaidu

4

7 Answers 7

3

The cleanest way to add the properties to the map would be (following on from your example):

for (String propName : properties.stringPropertyNames()) {
    map.put(propName, properties.getProperty(propName));
}

This works nicely in this particular case, because the Properties object is really a map containing String keys and values, as the getProperty method makes clear. It's only declared as Map<Object, Object> for horrible backwards-compatibility reasons.

By using the Properties-specific methods, rather than treating this as just a Map<Object, Object>, you can populate your Map<String, String> with perfect type-safety (rather than having to cast).

Sign up to request clarification or add additional context in comments.

1 Comment

I was curious about the reasons for Properties extending HashMap<Object, Object> and found this stackoverflow.com/questions/873510/…
3

You can directly convert:

Properties properties = new Properties();
Map<String, String> map = new HashMap<String, String>((Map)properties);

Comments

3
Map<String, String> properties2Map(Properties p) {
            Map<String, String> map = new HashMap<String, String>();
            for(Map.Entry<Object, Object> entry : p.entrySet()) {
                String key = (String) entry.getKey(); //not really unsafe, since you just loaded the properties
                map.put(key, p.getProperty(key));
            }
            return map;
}

I also like to use utility methods with type arguments to walk around generic type invariance and do some "downcasting" or "upcasting" (when I KNOW it is safe to do so). In this case:

@SuppressWarnings("unchecked")
<A, B extends A> Map<B, B> downCastMap(Map<A,A> map) {
    return (Map<B, B>)map;
}

Then you can write

Properties p = ...
Map<String, String> map = downCastMap(p);

2 Comments

can i put it like Map<String, String> map = (Map) properties;
Yes, suppressing rawtypes and unchecked. However, the utility method is more code documenting for anyone else (and yourself) reading the code.
1

Because we know that the Properties are a String-to-String mapping already, it's save to do it with rawtype and unchecked conversion. Just leave a comment:

    Properties properties = new Properties();    
    properties.load(instream); 

    @SuppressWarnings({ "rawtypes", "unchecked" })
    // this is save because Properties have a String to String mapping
    Map<String, String> map = new HashMap(properties);

3 Comments

Yes, but why create a new HashMap instance in the process?
Because we can't change or cast an instance of Properties to the type Map<String, String> because it implements the interface Map<Object, Object>.
Have a look at my answer above and the comments.
1

With Java 8 and the addition of Streams, I would suggest you to use the APIs Steam provides

Here, we assuming that each of the values actually are String objects, the cast to String should be safe:

Map<String,Object> map = new HashMap<>();

Map<String,String> stringifiedMapSafe = map.entrySet().stream()
     .collect(Collectors.toMap(Map.Entry::getKey, e -> (String)e.getValue()));

Now, in case we not sure that all the element are String, and we want to filter the key/values with null:

Map<String,Object> map = new HashMap<>();

Map<String,String> stringifiedMapNotSafe = map.entrySet().stream()
             .filter(m -> m.getKey() != null && m.getValue() !=null)
             .collect(Collectors.toMap(Map.Entry::getKey, e -> (String)e.getValue()));

Comments

0
Map<String,String> getPropInMap(Properties prop){       
    Map<String, String> myMap = new HashMap<String, String>();
    for (Object key : prop .keySet()) {
        myMap.put(key.toString(), prop .get(key).toString());
    }       
    return myMap;
}

Comments

0

Iterate over Map Object, take k v, make them string and put it to a Map String.

Map<Object,Object> map1; // with object k,v
Map<String, String> mapString = new HashMap<String, String>();
for (Object key : map1.keySet()) {
                String k = key.toString();
                String v = mmap1.get(key).toString();
                mapString.put(k, v);
            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.