java - 读取文件并将其内容转换为 HashMap 时出现“警告:[未选中] 未选中的转换”
问题描述
我正在阅读一个我以前写为 HashMap<String,String> 的文件。现在,我必须再读一遍,但它给了我“警告:[unchecked] unchecked conversion”,而且它没有编译。
导致错误的代码行:
HashMap<String,String> map_stage = Utils.readObject(staging_area, HashMap.class);
staging_area 是一个文件 obj,工作正常。左侧的 HashMap<String,String> 类型和右侧的 HashMap.class 之间的警告令人感叹
警告:
required: HashMap<String,String>
found: HashMap
Repository.java:156: warning: [unchecked] unchecked conversion
HashMap<String,String> map_stage = Utils.readObject(staging_area, HashMap.class);
^
这是 Utils.readObject:
/** Return an object of type T read from FILE, casting it to EXPECTEDCLASS.
* Throws IllegalArgumentException in case of problems. */
static <T extends Serializable> T readObject(File file,
Class<T> expectedClass) {
try {
ObjectInputStream in =
new ObjectInputStream(new FileInputStream(file));
T result = expectedClass.cast(in.readObject());
in.close();
return result;
} catch (IOException | ClassCastException
| ClassNotFoundException excp) {
throw new IllegalArgumentException(excp.getMessage());
}
}
^ 这个方法是给我的,所以你可以假设是正确的。
编译器错误:
grader/submit/AGTester.java:90: error: unreported exception IOException; must be caught or declared to be thrown
Main.main(args);
^
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
解决方案
我们找到了以下解决方法:
parent_commit_map = (HashMap <String, String>) readObject(parent_commit_file)
我不会提倡最好的实现,但它解决了问题。谢谢大家的建议!
推荐阅读
- javascript - 如何在firestore子集合中选择超过某个日期的文档?
- html - 如何使网页内容适合 100% iframe 中的 div
- php - Wordpress - 在函数文件中使用 Dompdf
- python - 当请求输入是元素列表时,蝗虫发布请求错误
- javascript - 在单行中对齐两个表
- javascript - 使用 Firebase 登录 Microsoft Teams 自定义选项卡
- r - R for lm 中的 autoplot():为什么我得到“恒定杠杆:残差与因子水平”而不是“残差与杠杆”图?
- c# - CollectionViewController 使用 c# 显示黑色背景,没有图像
- sql - ALL_TABLES、count(*) 和 count(PRIMARY_KEY) 有什么区别?
- sql - 如何获取与组中最新日期对应的值以填充 Oracle SQL 中的空值?