Map<String, Object> map = new HashMap<>();
 map.put("score", 10);
BigDecimal score = (BigDecimal) map.get("score");
System.out.println(score);

取值直接强转报错

Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.math.BigDecimal

正确的处理办法

BigDecimal score2 = new BigDecimal((Integer) map.get("score"));
System.out.println(score2);

后记:map中的Long类型转Integer处理办法

Integer aaa = ((Long) map.get("aaa")).intValue();

如果是double类型处理:

BigDecimal score = BigDecimal.valueOf((Double) paramMap.get("score"));

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐