如何遍历 TreeMap? [英] How to iterate over a TreeMap?
本文介绍了如何遍历 TreeMap?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能的重复:
如何遍历地图中的每个条目?
我想遍历 TreeMap
,并且对于所有具有特定值的键,我希望将它们添加到新的 TreeMap
.我该怎么做?
I want to iterate over a TreeMap
, and for all keys which have a particular value, I want them to be added to a new TreeMap
. How can I do this?
推荐答案
假设类型 TreeMap
Assuming type TreeMap<String,Integer> :
for(Map.Entry<String,Integer> entry : treeMap.entrySet()) {
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key + " => " + value);
}
(键和值类型当然可以是任何类)
(key and Value types can be any class of course)
这篇关于如何遍历 TreeMap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文