java - 使用可迭代到 Hashmap 的迭代器实现 - 可能吗?

标签 java hashmap iterable

如果不保留顺序,如何为 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/

相关文章:

java - 这段代码中何时发生 DNS 查找?通过 getByName() 或 getHostName() 或 getHostAddress()?

java - 覆盖方法和动态绑定(bind)

java - 在 Java Collections Map<Key,?> 中 "?"指的是什么?

java - 高效地遍历 HashMap 中的所有匹配键?

python - 在 Python 中组合两种不同的可迭代类型

java - 如何根据一组值更新数据模型

java - 用字符串中的字符填充数组

java - CTRL+C w/Spring Boot & Gradle 杀死 Gradle 守护进程

javascript - JavaScript 队列的 O(1) 删除

java - 用 Java 为二叉树编写通用迭代器