java - 为什么迭代条目集不起作用?
问题描述
我尝试像这样迭代 EntrySet:
for (Entry<A, List<B>> list : service.entrySet()) {
if (list.getKey() == typ1) {
for (B current : list.getValue()) {
// do sth
}
}
} else {
PrintHelper.printOut("not implemented case"
+ list.getKey());
}
}
}
即使我有那部分,if (list.getKey() == typ1)
我仍然得到印刷的案例not implemented case typ1
。
为什么会这样?我在迭代/ if 案例中做错了什么?
解决方案
地图(或地图条目)的键是一个对象 - 您需要将其与 进行比较equals
,而不是==
:
if (list.getKey().equals(typ1)) {
推荐阅读
- python - scrapy 发送电子邮件并附加带有抓取项目的 csv 文件
- javascript - 如何检查到目前为止输入的正则表达式在 JavaScript 中是否正确?
- node.js - 以前可以工作的 npm install 现在不行了
- css - 将鼠标悬停在日期上时如何在反应日历中显示日期的某些内容
- laravel - 在评论instagram graph-api 上获取个人资料图片其他帐户| 脸书defeloper
- flutter - Flutte: update current user after signin with Firebase
- react-native - 如何在使用 TabView 的 React Native 中移动文本单击的下一个选项卡?
- pytorch - RuntimeError:无法运行“aten::xxxx”,无法加载使用 TPU 训练的 Pytorch 模型
- javascript - 在 Javascript 中将图像 url 转换为 blob
- php - Laravel 多条件数据库更新