当前位置: 移动技术网 > IT编程>开发语言>Java > map实现按value升序排序

map实现按value升序排序

2019年07月22日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下: /**     * @param h     * @retu

复制代码 代码如下:

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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网