- Create a (String, Object) hashMap and put values of different types.
- Serialize the hashmap into a json string and convert back into a hashmap of (String, String)
When I try to get the value of key "key2", it will throw an exception saying "Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String".
Is there a good way to explain this? Does it mean even though I have a (String, String) map, the value in it is not necessary String?
Apologize for any confusion. Let me know if something is not clear.
Map<String, Object> map = new HashMap<>();
map.put("key1", "value1");
map.put("key2", 99);
map.put("key3", new Date());
JsonUtil jsonUtil = new JsonUtil();
String s = jsonUtil.toJson(map);
HashMap<String, String> newMap = jsonUtil.fromJson(s, HashMap.class);
String value = newMap.get("key2");
HashMap.classis required as a type literal(*) but can't take generic parameters.