消除unchecked cast Warning

简介: 消除unchecked cast Warning

将一个Object类型转成自己想要的Map、List等,会出现 unchecked cast Warning 警告。

Map<String, String> castMap = (HashMap<String, String>) obj;


解决办法,添加一个方法:

@SuppressWarnings("unchecked")
public static <T> T cast(Object obj) {
    return (T) obj;
}


使用:

Map<String, String> row = cast(JSONObject.parse(rowEntity));
相关文章
|
5月前
Flutter-解决Try catch出现异常:type ‘_TypeError‘ is not a subtype of type ‘Exception‘ in type cast
Flutter-解决Try catch出现异常:type ‘_TypeError‘ is not a subtype of type ‘Exception‘ in type cast
103 1
C++ 编译错误 error: ‘cout‘ was not declared in this scope (摄氏度与华氏度的转换)
C++ 编译错误 error: ‘cout‘ was not declared in this scope (摄氏度与华氏度的转换)
C++ 编译错误 error: ‘cout‘ was not declared in this scope (摄氏度与华氏度的转换)
|
7月前
|
编译器 C++ 开发者
C++一分钟之-返回值优化与Move Semantics
【6月更文挑战第19天】C++的RVO与移动语义提升效率,减少对象复制。RVO是编译器优化,避免临时对象的创建。移动语义通过右值引用和`std::move`转移资源所有权。注意RVO不是总有效,不应过度依赖。使用移动语义时,避免误用`std::move`导致对象无效。示例展示了RVO和移动构造函数的应用。理解并恰当使用这些机制能写出更高效代码。
76 3
|
8月前
|
C++
[C++] 强制类型转换(dynamic_cast和dynamic_Pointer_cast)
[C++] 强制类型转换(dynamic_cast和dynamic_Pointer_cast)
135 1
|
8月前
|
数据库
Greenplum【异常 03】COPY命令报错 > ERROR: invalid input syntax for type double precision: ““(问题分析及解决方案)数据去重
Greenplum【异常 03】COPY命令报错 > ERROR: invalid input syntax for type double precision: ““(问题分析及解决方案)数据去重
238 0
|
8月前
|
安全 编译器 程序员
[C++ 从入门到精通] 6.static_cast、dynamic_cast等显示类型转换
[C++ 从入门到精通] 6.static_cast、dynamic_cast等显示类型转换
109 0
|
安全 C语言
警告 1 warning C4996: ‘scanf‘: This function or variable may be unsafe.
警告 1 warning C4996: ‘scanf‘: This function or variable may be unsafe.
|
关系型数据库 MySQL C++
类型收窄 error C2397: conversion from ‘const int‘ to ‘char‘ requires a narrowing conversion
类型收窄 error C2397: conversion from ‘const int‘ to ‘char‘ requires a narrowing conversion
210 0
|
Java 编译器
规避Variable used in lambda expression should be final or effectively final而引发了方法参数值拷贝的问题
规避Variable used in lambda expression should be final or effectively final而引发了方法参数值拷贝的问题
198 0
解决办法:Type safety: The expression of type List needs unchecked conversion to conform
解决办法:Type safety: The expression of type List needs unchecked conversion to conform
346 0