How can I convert Map
to Map
?
This does not work:
Map map = ne
As you are casting from Object to String I recommend you catch and report (in some way, here I just print a message, which is generally bad) the exception.
Map map = new HashMap(); //Object is containing String
Map newMap =new HashMap();
for (Map.Entry entry : map.entrySet()) {
try{
newMap.put(entry.getKey(), (String) entry.getValue());
}
catch(ClassCastException e){
System.out.println("ERROR: "+entry.getKey()+" -> "+entry.getValue()+
" not added, as "+entry.getValue()+" is not a String");
}
}