如果不保留顺序,如何为 Hashmap 创建迭代器(通过实现可迭代)? 我的键应该是有序的..我想按降序迭代
最佳答案
使用 TreeMap
也许:
The map is sorted according to the natural ordering of its keys
此外,来自 TreeMap.keySet()
文档:
Returns a Set view of the keys contained in this map. The set's iterator returns the keys in ascending order
迭代示例:
TreeMap<K,V> tree;
// ...
for (final String key : tree.keySet()) {
final V value = tree.get(key);
}
关于java - 使用可迭代到 Hashmap 的迭代器实现 - 可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27198636/