Map.Entry

实例如下:

public String getReportURL() throws Exception {
        String reportURL = parameterMap.get("showPageUrl").toString() + "showPageNum=1";
        Iterator it = parameterMap.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            Object keyObj = entry.getKey();
            Object valObj = entry.getValue();
            if (keyObj != null && keyObj.toString().length() > 0 && valObj != null && valObj.toString().length() > 0) {
                String key = keyObj.toString();
                String val = valObj.toString();
                if (!"showPageNum".equals(key))
                    reportURL += "&" + URLEncoder.encode(key, "gb2312") + "=" + URLEncoder.encode(val, "gb2312");
            }
        }
        return reportURL;
    }

 

 Map类提供了一个称为entrySet()的方法,这个方法返回一个Map.Entry实例化后的对象集。 接着,Map.Entry类提供了一个getKey()方法和一个getValue()方法

 

同时,提供给开发人员一个同时保持了关键字和其对应的值的类。Map.Entry同时也提供了一个setValue()方法,程序员可以使用它修改map里面的值。

map内部是按照hash算法存储的,但如果能对map排序在某些时候还是有用的

/**
     * @param h
     * @return
     * 实现对map按照value升序排序
     */
    @SuppressWarnings("unchecked")
    public static Map.Entry[] getSortedHashtableByValue(Map h) {
        Set set = h.entrySet();
        Map.Entry[] entries = (Map.Entry[]) set.toArray(new Map.Entry[set .size()]);
        Arrays.sort(entries, new Comparator() {
            public int compare(Object arg0, Object arg1) {
                Long key1 = Long.valueOf(((Map.Entry) arg0).getValue().toString());
                Long key2 = Long.valueOf(((Map.Entry) arg1).getValue().toString());
                return key1.compareTo(key2);
            }
        });

        return entries;
    }

/**
     * @param h
     * @return
     * 实现对map按照key排序
     */
    @SuppressWarnings("unchecked")
    public static Map.Entry[] getSortedHashtableByKey(Map h) {

        Set set = h.entrySet();

        Map.Entry[] entries = (Map.Entry[]) set.toArray(new Map.Entry[set.size()]);

        Arrays.sort(entries, new Comparator() {
            public int compare(Object arg0, Object arg1) {
                Object key1 = ((Map.Entry) arg0).getKey();
                Object key2 = ((Map.Entry) arg1).getKey();
                return ((Comparable) key1).compareTo(key2);
            }

        });

        return entries;
    }

 

 

 

Map的keySet方法

2008-07-12 10:44

 

有一个Map对象,这时候使用keySet()方法获取所有的key值,比如:

   Map map = new HashMap();

   map.put(1, "a");

   map.put(2, "b");

   map.put(3, "c");

   map.put(4, "d");

   Set keys1 = map.keySet();

   Set keys2 = map.keySet();

   Set keys3 = map.keySet();

上面三个set对象key1,key2,key3引用的是一个对象。这是map的keySet()方法只返回一个set实例,所以当从key1中删除一个对象时候,key2和key3将会受到影响。

   keys1.remove(1);

   System.out.println(keys1);

   System.out.println(keys2);

   System.out.println(keys3);

打印结果为:

[2, 4, 3]

[2, 4, 3]

[2, 4, 3]

 

在使用map时有时会出现此类的错误

    java.util.ConcurrentModificationException 

 

在执行如下 代码时会出此错误:

  Set<String> keySet=map.keySet();

for(String key:keySet){

    map.remove(key);

}

原因是删除一个Entry之后keySet集合不对,在执行循环时会出现错误。将代码改为如下则能解决此问题:

String[]  keySet=map.keySet().toArray(new String[0])

for(String key:keySet){

    map.remove(key);

}

 

因为加了toArray所以新的集合和原来的keySet就没有了关系,所以也就不会出错了

posted @ 2014-08-06 14:52  破冰Lab-Cookun  阅读(384)  评论(0编辑  收藏  举报