如何遍历有序的TreeMap

haorengoodman 2014-08-04 02:48:56

public static void main(String[] args) {
TreeMap<String, String> sPara = new TreeMap<String, String>();
sPara.put("abc", "abc");
sPara.put("cds", "cds");
sPara.put("ads","ads");
sPara.put("fff", "fff");
sPara.put("ccc", "ccc");
sPara.put("ddd", "ddd");
sPara.put("qqqqq", "qqqqq");
sPara.put("knns", "knns");
sPara.put("ioio", "ioio");
Set<String> keys = sPara.keySet();
for (String key : keys){
System.out.println(key);
}
}

结果:
abc
ads
ccc
cds
ddd
fff
ioio
knns
qqqqq
问题:
TreeMap 默认按照key的自然顺序排序,
但是遍历的时候key被放入到一个Set集合中,Set集合本身是无序的,为什么结果仍然有序?

...全文
7575 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
haorengoodman 2014-08-05
  • 打赏
  • 举报
回复
引用 1 楼 rui888 的回复:
/** * Returns a {@link Set} view of the keys contained in this map. * The set's iterator returns the keys in ascending order. * The set is backed by the map, so changes to the map are * reflected in the set, and vice-versa. If the map is modified * while an iteration over the set is in progress (except through * the iterator's own <tt>remove</tt> operation), the results of * the iteration are undefined. The set supports element removal, * which removes the corresponding mapping from the map, via the * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> * operations. It does not support the <tt>add</tt> or <tt>addAll</tt> * operations. */ public Set<K> keySet() { return navigableKeySet(); }
谢啦,看了一下源码,navigableKeySet 类型是 KeySet ,实现了SortedSet接口,所以最后拿到的set集合就是有序的了

static final class KeySet<E> extends AbstractSet<E> implements NavigableSet<E>{......}
public interface NavigableSet<E> extends SortedSet<E>{......}
小绵羊 2014-08-05
  • 打赏
  • 举报
回复
没有谁说过Set是无序的啊。 Set只保证元素的唯一性。
xiaofeifeiloving 2014-08-04
  • 打赏
  • 举报
回复
Map map=new TreeMap();//创建集合对象 Iterator ir=map.keySet().iterator();//获取hashMap的键值,并进行遍历 while(ir.hasNext()){ Object key= ir.next(); System.out.println("键为"+key+"所对应的值为"+map.get(key)); } 即是通过TreeMap中指针的移动,实现对TreeMap的遍历
zy_think123 2014-08-04
  • 打赏
  • 举报
回复
String类型以及基本数据类型都会给你排好序的,但是自定义类型就是无序了,需要自己实现比较器,可以考虑实现comparable或者compartor接口
a137655624 2014-08-04
  • 打赏
  • 举报
回复
TreeSet 底层是通过 TreeMap 来实现的 TreeSet set = TreeMap.getKeySet(); 这样 set 就是默认有序的
tony4geek 2014-08-04
  • 打赏
  • 举报
回复
里面是有序的。
tony4geek 2014-08-04
  • 打赏
  • 举报
回复
/** * Returns a {@link Set} view of the keys contained in this map. * The set's iterator returns the keys in ascending order. * The set is backed by the map, so changes to the map are * reflected in the set, and vice-versa. If the map is modified * while an iteration over the set is in progress (except through * the iterator's own <tt>remove</tt> operation), the results of * the iteration are undefined. The set supports element removal, * which removes the corresponding mapping from the map, via the * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> * operations. It does not support the <tt>add</tt> or <tt>addAll</tt> * operations. */ public Set<K> keySet() { return navigableKeySet(); }
资源下载链接为: https://pan.quark.cn/s/1bfadf00ae14 在 Java 开发中,选择合适的 Map 实现类是常见的问题。HashMap 和 TreeMap 是两种常用的 Map 实现类,它们各有特点和适用场景,以下是它们的对比和选择建议。 HashMap 是基于哈希表实现的 Map,通过哈希函数将键映射到数组的索引位置,相同索引位置的键值对以链表形式存储。其查询速度快,能在平均常数时间内完成键值对的查找,适合快速检索场景。HashMap 的优点包括: 查询效率高,可快速定位键对应的值。 实现机制简单,易于理解和使用。 然而,HashMap 也有不足: 不支持键的排序,键值对的存储顺序是无序的。 若哈希冲突过多,性能会下降。 TreeMap 是基于红黑树实现的 Map,它将键值对存储在红黑树中,通过树的结构保持键的有序性。其优点是: 能保持键的有序排列,支持自然排序或自定义排序。 键值对的顺序固定,便于有序遍历。 但其缺点也很明显: 查询速度较 HashMap 慢,因为需要维护红黑树的平衡。 实现复杂度较高,性能开销较大。 快速查询优先:如果主要需求是快速查找键值对,且不关心键的顺序,应选择 HashMap。 排序需求优先:如果需要按键值对的键进行排序,或者频繁进行有序遍历,应选择 TreeMap。 插入删除频繁:对于频繁插入和删除操作的场景,HashMap 的性能通常更优。 遍历有序性重要:当需要有序遍历键值对时,TreeMap 是更好的选择。 总之,选择 HashMap 还是 TreeMap 应根据实际需求和使用场景来决定,权衡查询速度和排序需求,以满足应用的最佳性能和功能要求。

62,630

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧