解决方案 »

  1.   

     /**
         * 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();
        }
      

  2.   

    TreeSet 底层是通过 TreeMap 来实现的
    TreeSet  set = TreeMap.getKeySet();
    这样 set 就是默认有序的
      

  3.   

    String类型以及基本数据类型都会给你排好序的,但是自定义类型就是无序了,需要自己实现比较器,可以考虑实现comparable或者compartor接口
      

  4.   

    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的遍历
      

  5.   

    没有谁说过Set是无序的啊。
    Set只保证元素的唯一性。
      

  6.   

    谢啦,看了一下源码,navigableKeySet 类型是 KeySet ,实现了SortedSet接口,所以最后拿到的set集合就是有序的了static final class KeySet<E> extends AbstractSet<E> implements NavigableSet<E>{......}
    public interface NavigableSet<E> extends SortedSet<E>{......}