如何遍历有序的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集合本身是无序的,为什么结果仍然有序?

...全文
7446 7 打赏 收藏 转发到动态 举报
写回复
用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(); }

62,614

社区成员

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

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