我发现了这个并且它似乎在工作,我正在尝试通过 HashMap 进行迭代:
但这部分代码显示警告,我不知道如何让它不显示警告:
Iterator it = map.entrySet().iterator();
Map.Entry pairs = (Map.Entry) it.next();
有没有办法在不使用 suppressWarnings 注释的情况下“修复”这个问题?
最佳答案
是的,使用 it
和 pairs
的正确通用定义 - 假设 map
被正确定义。
例如:
Map<String, Integer> map = ...;
Iterator<Map.Entry<String, Integer>> it = map.entrySet().iterator();
Map.Entry<String, Integer> pairs = it.next(); // no need for cast
关于java - 如何删除警告java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5398774/