When from map < String,Object> The above exception occurs when the key value is taken out of
public static void main(String[] args) {
Map<String, Object> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
double value = (double) map.get("a");
System.out.println(value);
}
Map< String, Object> map = new HashMap<>() In order to avoid the above problems, we can judge by instanceof
public static void main(String[] args) {
Map<String, Object> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
Object object = map.get("a");
if (object instanceof Integer) {
System.out.println("true");
}
}
according to the console output, the return type of map.get (“a”) is known as integer
solution: use tostring()/string. Valueof() to convert it to string first and then to the required data type
public static void main(String[] args) {
Map<String, Object> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
Double a = Double.valueOf(map.get("a").toString());
Double b = Double.valueOf(map.get("b").toString());
System.out.println("a:" + a);
System.out.println("b:" + b);
}
Similar Posts:
- Convert Object to List>, avoiding Unchecked cast: ‘java.lang.Object’ to ‘java.util.List
- [Two Solutions] non-static method xxx() cannot be referenced from a static context
- Java – Convert bytes to unsigned bytes
- Solution to the problem of Java program “resource leak: ‘SC’ is never closed” in vscode
- Roman to Integer LeetCode Java
- [Solved] Java Call Error: java.lang.IllegalArgumentException: wrong number of arguments
- [Problems with the use of internal classes] No enclosing instance of type Outer is accessible. Must qualify the allocation with an enclo
- Java error: No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- [Solved] Exception in thread “main” java.util.ConcurrentModificationException
- Eclipse Error:The selection cannot be launched, and there are no recent launches