2017-10-10 89 views
1

我揣摩我如何能夠通過其鍵升序排列無痛HashMap中值迭代,下面不工作:遍歷其排序鍵是一個HashMap

HashMap buckets; 

for(String bucketKey : new TreeSet(buckets.keySet())) { 
    // actual code 
} 

回答

2

終於找到了一種方法通過使用ArrayList來做到這一點,但絕對不知道它是如何去的:

HashMap buckets; 
ArrayList l = new ArrayList(buckets.keySet()); 
Collections.sort(l); 
for(String bucketKey : l) { 
    // actual code 
}