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()));