java treemap value排序_Java TreeMap升序|降序排列和按照value进行排序的案例

TreeMap 升序|降序排列

import java.util.Comparator;

import java.util.TreeMap;

public class Main {

public static void main(String[] args) {

TreeMap map1 = new TreeMap(); //默认的TreeMap升序排列

TreeMap map2= new TreeMap(new Comparator(){

/*

* int compare(Object o1, Object o2) 返回一个基本类型的整型,

* 返回负数表示:o1 小于o2,

* 返回0 表示:o1和o2相等,

* 返回正数表示:o1大于o2。

*/

public int compare(Integer a,Integer b){

return b-a;

}

});

map2.put(1,2);

map2.put(2,4);

map2.put(7, 1);

map2.put(5,2);

System.out.println("Map2="+map2);

map1.put(1,2);

map1.put(2,4);

map1.put(7, 1);

map1.put(5,2);

System.out.println("map1="+map1);

}

}

TreeMap按照value进行排序

TreeMap底层是根据红黑树的数据结构构建的,默认是根据key的自然排序来组织(比如integer的大小,String的字典排序)。所以,TreeMap只能根据key来排序,是不能根据value来排序的(否则key来排序根本就不能形成TreeMap)。

今天有个需求,就是要根据treeMap中的value排序。所以网上看了一下,大致的思路是把TreeMap的EntrySet转换成list,然后使用Collections.sor排序。

代码:

public static void sortByValue() {

Map map = new TreeMap();

map.put("a", "dddd");

map.put("d", "aaaa");

map.put("b", "cccc");

map.put("c", "bbbb");

List> list = new ArrayList>(map.entrySet());

Collections.sort(list,new Comparator>() {

//升序排序

public int compare(Entry o1, Entry o2) {

return o1.getValue().compareTo(o2.getValue());

}

});

for (Entry e: list) {

System.out.println(e.getKey()+":"+e.getValue());

}

}

补充知识:使用比较器对Treemap按照value进行排序

使用比较器对Treemap按照value进行排序(value值只有是string类型时才适用)

有时我们需要根据TreeMap的value来进行排序。对value排序我们就需要借助于Collections的sort(List list, Comparator

public class MapSortDemo {

public static void main(String[] args) {

Map map = new TreeMap();

map.put("KFC", "kfc");

map.put("WNBA", "wnba");

map.put("NBA", "nba");

map.put("CBA", "cba");

Map resultMap = sortMapByKey(map); //按Key进行排序

// Map resultMap = sortMapByValue(map); //按Value进行排序

for (Map.Entry entry : resultMap.entrySet()) {

System.out.println(entry.getKey() + " " + entry.getValue());

}

}

/**

* 使用 Map按value进行排序

* @param map

* @return

*/

public static Map sortMapByValue(Map oriMap) {

if (oriMap == null || oriMap.isEmpty()) {

return null;

}

Map sortedMap = new LinkedHashMap();

List> entryList = new ArrayList>(

oriMap.entrySet());

Collections.sort(entryList, new MapValueComparator());

Iterator> iter = entryList.iterator();

Map.Entry tmpEntry = null;

while (iter.hasNext()) {

tmpEntry = iter.next();

sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());

}

return sortedMap;

}

}

//比较器

class MapValueComparator implements Comparator> {

@Override

public int compare(Entry me1, Entry me2) {

return me1.getValue().compareTo(me2.getValue());

}

}

方式二

public class TreeMapTest {

public static void main(String[] args) {

Map map = new TreeMap();

map.put("a", "ddddd");

map.put("c", "bbbbb");

map.put("d", "aaaaa");

map.put("b", "ccccc");

//这里将map.entrySet()转换成list

List> list = new ArrayList>(map.entrySet());

//然后通过比较器来实现排序

Collections.sort(list,new Comparator>() {

//升序排序

public int compare(Entry o1,

Entry o2) {

return o1.getValue().compareTo(o2.getValue());

}

});

for(Map.Entry mapping:list){

System.out.println(mapping.getKey()+":"+mapping.getValue());

}

}

}

运行结果如下:

d:aaaaa

c:bbbbb

b:ccccc

a:ddddd

以上这篇Java TreeMap升序|降序排列和按照value进行排序的案例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值