如何解决为什么遍历条目集不起作用?
我尝试像这样遍历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
。
为什么会这样?迭代/如果是情况,我怎么了?
解决方法
地图(或地图条目)的键是一个对象-您需要将其与equals
而不是==
进行比较:
if (list.getKey().equals(typ1)) {
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。