hashmap中keySet与entrySet遍历

Map<Integer, Map<String, Long>> map7 = 数据库查询结果;
//两种遍历
//第一种,用keySet遍历
for (Integer id : map7.keySet()) {
complaintCountMap.put(id, complaintCountMap.get(id) + map7.get(id).get("complaintCount"));
}
//第二种,用entrySet遍历
for (Map.Entry<Integer, Map<String, Long>> entry : map7.entrySet()) {
complaintCountMap.put(entry.getKey(), complaintCountMap.get(entry.getKey()) + entry.getValue().get("complaintCount"));
}

推荐使用第二种,因为第二种方法可以直接把键值取出,而第一种需要多一步get操作
posted @ 2020-07-15 16:46  菲菲一个  阅读(238)  评论(0编辑  收藏  举报