是不是println一个Map的EntrySet,是不是把这个Map都打印出来,对EntrySet排序后,打印这个EntrySet是不是也是排序好的Map呢?

解决方案 »

  1.   

    没明白你问的。。什么是Map的EntrySet??Map<Integer,String> m = new HashMap<Integer,String>();
    m.put(1,"one");
    m.put(2,"two");
    m.put(3,"three");
    System.out.println(m);
      

  2.   

    我也不明白。
    这个方法返回的是Set 我记得set是无序不可重复的
      

  3.   


    显然。Map里不会有重复的KEY。
    我也不是很清楚MAP.ENTRYSET是什么东西。
    但有下面概念,应该可以帮助你理解。
    如果是HASHMAP应该是散列的,无序。
    TREEMAP有顺序。
    LINKEDHASHMAP是HASHMAP一种。
    但是TREEMAP应该是按其键字排序的。不知道你指的排序是什么。也有可能我目前所知有限。
    所以有顺序的仅仅是TREEMAP。
    而entry是一个set他应该也是散列的,无序。
      

  4.   

    我建议楼主有各种MAP打印ENTRYSET出来看看
    SET也有HASHSET和TREESET。后者显然有顺序
      

  5.   

    给你提供一个map的遍历方法
    for (Iterator iter = sectionIndexMap.entrySet().iterator(); iter.hasNext();)
    {
    Map.Entry<String, Section> entry = (Map.Entry<String, Section>) iter
    .next();
    String secname = entry.getKey();//输出关键字的值 Section sec = entry.getValue();//关键字的映射的值
    sectinIndexMap是一个LinkedHashMap此MAP是有序的,按照你存入的顺序进行排序,其余的map都是无序的
      

  6.   

    sectinIndexMap是一个LinkedHashMap此MAP是有序的,按照你存入的顺序进行排序,其余的map都是无序的学习了但treemap也是无序的吗?
      

  7.   

    entry,是map当中具体存放数据的东西了。
    transient Entry[] table;
      

  8.   

     treemap是有序的 , hashmap是无序的
      

  9.   

    我还是纠结,干脆把代码都贴出来,有空的大侠帮看看。import java.util.*;public class SortingExample2 {
       public static void main(String[] args)
          throws java.io.FileNotFoundException {
         
        Map word_count_map = new WordCountMap(args[0]);
        //上面建立了一个 Map, key是单词,value是该单词在文章中出现的次数    Set entry_set = word_count_map.entrySet(); //这个就是那个entrySet    System.out.println("结果1:Unsorted Entry Set:\n" + entry_set););//就是这个,打印出来结果1
        List entry_list = new ArrayList(entry_set);    Collections.sort(entry_list, new AlphaComparator());//按单词字母顺序排序
        System.out.println("\n结果2:Entry Set (sorted alpha):\n" + entry_list)//就是纠结这个,为什么打印一个Map的EntryList,却能打印出整个Map?
        
        Collections.sort(entry_list, new FreqComparator());//按单词出现频率排序
        System.out.println("\n结果3:Entry Set (sorted by freq):\n" + entry_list);
     }private static class AlphaComparator implements Comparator {
      public int compare(Object e1, Object e2) {
        String word1 = (String) ((Map.Entry) e1).getKey();
        String word2 = (String) ((Map.Entry) e2).getKey();
        return word1.compareTo(word2);
      }
    }private static class FreqComparator implements Comparator {
      public int compare(Object e1, Object e2) {
        Integer freq1 = (Integer) ((Map.Entry) e1).getValue();
        Integer freq2 = (Integer) ((Map.Entry) e2).getValue();
        return freq2.compareTo(freq1);
      }
    }
    结果1:Unsorted Entry Set:
    [unclean=1, with=2, scene=1, passage=1, our=3, ancient=1, two=3, these=1, ’d=1,
    patient=1, do=1, cross’d=1, where=2, lovers=1, fatal=1, stage=1, verona=1, new=1,
    bury=1, forth=1, strife=1, lay=1, fair=1, we=1, alike=1, could=1, piteous=1, is=1,
    hands=1, mend=1, in=2, nought=1, both=1, continuance=1, life=1, if=1, shall=2, the=5,
    traffic=1, and=1, a=1, toil=1, take=1, which=2, loins=1, of=5, here=1, end=1, what=1,
    civil=2, their=6, love=1, but=1, makes=1, miss=1, rage=1, foes=1, you=1, ears=1,
    whose=1, now=1, to=2, dignity=1, fearful=1, pair=1, star=1, strive=1, households=1,
    hours’=1, grudge=1, break=1, misadventured=1, mutiny=1, attend=1, overthrows=1,
    parents’=2, blood=1, from=2, children’s=1, remove=1, death=2]结果2:Entry Set (sorted alpha):
    [a=1, alike=1, ancient=1, and=1, attend=1, blood=1, both=1, break=1, bury=1, but=1,
    children’s=1, civil=2, continuance=1, could=1, cross’d=1, death=2, dignity=1, do=1,
    ears=1, end=1, fair=1, fatal=1, fearful=1, foes=1, forth=1, from=2, grudge=1, hands=1,
    here=1, hours’=1, households=1, if=1, in=2, is=1, lay=1, life=1, loins=1, love=1,
    lovers=1, makes=1, ’d=1, mend=1, misadventured=1, miss=1, mutiny=1, new=1, nought=1,
    now=1, of=5, our=3, overthrows=1, pair=1, parents’=2, passage=1, patient=1, piteous=1,
    rage=1, remove=1, scene=1, shall=2, stage=1, star=1, strife=1, strive=1, take=1, the=5,
    their=6, these=1, to=2, toil=1, traffic=1, two=3, unclean=1, verona=1, we=1, what=1,
    where=2, which=2, whose=1, with=2, you=1]结果3:Entry Set (sorted by freq):
    [their=6, of=5, the=5, our=3, two=3, civil=2, death=2, from=2, in=2, parents’=2,
    shall=2, to=2, where=2, which=2, with=2, a=1, alike=1, ancient=1, and=1, attend=1,
    blood=1, both=1, break=1, bury=1, but=1, children’s=1, continuance=1, could=1,
    cross’d=1, dignity=1, do=1, ears=1, end=1, fair=1, fatal=1, fearful=1, foes=1, forth=1,
    grudge=1, hands=1, here=1, hours’=1, households=1, if=1, is=1, lay=1, life=1, loins=1,
    love=1, lovers=1, makes=1, ’d=1, mend=1, misadventured=1, miss=1, mutiny=1, new=1,
    nought=1, now=1, overthrows=1, pair=1, passage=1, patient=1, piteous=1, rage=1,
    remove=1, scene=1, stage=1, star=1, strife=1, strive=1, take=1, these=1, toil=1,
    traffic=1, unclean=1, verona=1, we=1, what=1, whose=1, you=1]
      

  10.   

    EntrySet 我想也是一个遍历map时的一个方法,生成一个Set。这就要看你用的map是什么map了如果是SortedMap的就是排好序的了!
      

  11.   

    首先建议楼主去看JDK源码,估计这么说也不是很清楚。。发现楼主要问的好像是ENTRY里面是什么内容
    好吧
    Map.entrySet()返回的是什么玩意呢?一个Set。
    具体如下:
    entrySet = new AbstractSet<Map.Entry<K,V>>() {}
    是一个继承自AbstractSet匿名内部类。
    Set里面放嘛玩意呢?一种JAVA的静态内部类:Map.Entry的对象集合。
    Map.Entry是个嘛玩意?
    看看结构:
    static class Entry<K,V> implements Map.Entry<K,V> {
    K key;
            V value;
            Entry<K,V> left = null;
            Entry<K,V> right = null;
            Entry<K,V> parent;
    。}
    有Key 有Value 和Map结构很类似
    关键它有个供打印用的方法:
            public String toString() {
                return key + "=" + value;
            }
    嗯 到这里有点眉目了
    前面说了,EntrySet对象是继承自AbstractSet的匿名类实例。
    所以还要去看AbstractSet的toString方法。实现效果和打印Set时候的效果一样,左右有方框。而Map的打印效果是大括号,这是因为是在AbstractMap里控制实现的。现在可以理解为什么打印map.entrySet(),为什么是[key=value,key=value,key=value]
    的形式了。那么它的值是怎么来的。再来看这个Map.Entry的key和value是怎么来的:以TreeMap为例子(楼主的Map应该也是继承这几类)
    TreeMap的构造器中 
        public TreeMap(Map<? extends K, ? extends V> m) {
            putAll(m);
        }
    而putAll中最后有这么一句
        super.putAll(map);
    在父类AbstractractMap中如下代码
             while (i.hasNext()) {
        Entry<? extends K, ? extends V> e = i.next();     put(e.getKey(), e.getValue());
    }在初始化这个Map的时候Map.entry也被赋值了。具有看上去一样的Key和Value。但它(Map.entry)不是Map。只是打印出来看上去像。所以,第一个打印,entrySet是以Map.entry为对象的Set。
    Map.Entry。Map.Entry的内容在Map被创建出来的时候就有了,并且值在外观上去这个Map一样。事实上就是就是通过这个entrySet来获取和操作Map里关于Key和Value的信息。所以打印的时候就将其Map所有内容打印出来了。
    关于第2个和第3个的排序,和Set和Map都无关了。已经是一个ArrayList对象了。ArrayList能接受任意一个继承自Collection对象,将其转为ArrayList,前提是这个继承自Collection对象实现了toArray方法。
    显然entrySet的“长辈类”里有这个方法:
    在AbstractCollection可以找到:
        public Object[] toArray() {
    Object[] result = new Object[size()];
    Iterator<E> e = iterator();
    for (int i=0; e.hasNext(); i++)
        result[i] = e.next();
    return result;
        }
    entrySet里有
    public Iterator<Map.Entry<K,V>> iterator() {
                        return new EntryIterator();
                    }
    关系有些复杂,楼主自己去找JDK代码看比较好!总之2,3得到的是存放了Map.entry对象的ArrayList。与Set已经无关。写的有些乱糟糟。要是看不懂,那你只要知道,后面是把一个从Set拷贝到ArrayList,2,3的操作与Set根本无关。
      

  12.   

    上面是我自己理解过程,为回答楼主问题楼主只要知道:后面给ArrayList排序并没有给Set排序。
    为什么能打印出来整个Map的值,是因为,
    ArrayList里放的是前面得到的Map.Entry对象,这个对象里包含有Map的Key和Value。(Map初始化的时候,自动给Map.Entry赋了值了。它的值是这么来的。)
    排序代码里不也是拿Map.Entry来比较处理的吗?不都getKey和getValue的吗?直接说明这个对象里有key和value。所以打印ArrayList时候把Key和Value打印出来了。
      

  13.   

    自己还有些不理解的地方,做个标记,明天有时间再看。
    Set里到底是一个Map.entry还是有多个Map.entry?
    那么楼主的代码里
    List entry_list = new ArrayList(entry_set);
    转成ArrayList后,是得到了个啥?由一个Map.entry还是多个Map.entry组成的ArrayList?
    从效果来看,应该是多个
    怎么是多个呢?应该是一个阿。难道一个Key一个Value成为了实际一个对象?在ArrayList里变成了一个个具体的键值对?这个挺怪的。