TreeMap 升序|降序排列

import java.util.Comparator;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
TreeMap<Integer,Integer> map1 = new TreeMap<Integer,Integer>(); //默认的TreeMap升序排列
TreeMap<Integer,Integer> map2= new TreeMap<Integer,Integer>(new Comparator<Integer>(){
/*
* 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<String,String> map = new TreeMap<String,String>();
map.put("a", "dddd");
map.put("d", "aaaa");
map.put("b", "cccc");
map.put("c", "bbbb"); List<Entry<String, String>> list = new ArrayList<Entry<String, String>>(map.entrySet()); Collections.sort(list,new Comparator<Map.Entry<String,String>>() {
//升序排序
public int compare(Entry<String, String> o1, Entry<String, String> o2) {
return o1.getValue().compareTo(o2.getValue());
}
}); for (Entry<String, String> e: list) {
System.out.println(e.getKey()+":"+e.getValue());
}
}

TreeMap升序|降序排列和按照value进行排序的更多相关文章

  1. js学习篇--数组按升序降序排列

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. C++员工管理系统(封装+多态+继承+分类化+函数调用+读写文件+指针+升序降序算法等一系列知识结合)

    1 C++职工管理系统 2 该项目实现 八个 功能 3 1-增加功能 2-显示功能 3-删除功能 4-修改功能 4 5-查找功能 6-排序功能 7-清空功能 8-退出功能 5 实现多个功能使用了多个C ...

  3. 笔记:mysql升序排列asc,降序排列desc

    经常会忘记mysql中升序和降序用什么字符来表示,现在就做个笔记:升序排列asc,降序排列desc,举个例子,下面是按时间降序调用栏目的文章,也即是栏目最新文章 [e:loop={"sele ...

  4. mapreduce程序的按照key值从大到小降序排列

    在近期的Hadoop的学习中,在学习mapreduce时遇到问题:让求所给数据的top10,们我们指导mapreduce中是有默认的排列机制的,是按照key的升序从大到小排列的 然而top10问题的求 ...

  5. 转<<C#集合Dictionary中按值的降序排列

    转载地址:http://blog.sina.com.cn/s/blog_5c5bc9070100pped.html C#集合Dictionary中按值的降序排列 static void Main(st ...

  6. MSSQL 随机查询且降序排列

    --随机查询且降序排列 * FROM dbo.COMPANY_USER_INFO ORDER BY NEWID()) AS T ORDER BY T.cu_id DESC

  7. Shell练习 统计单词个数,降序排列

    原文:https://leetcode.com/problems/word-frequency/ Write a bash script to calculate the frequency of e ...

  8. C# List.sort排序详解(多权重,升序降序)

    很多人可能喜欢Linq的orderBy排序,可惜U3D里面linq在Ios上会报错,所以就必须使用list的排序. 其实理解了并不难 升序降序比较 sort有三种结果 1,-1,0分别是大,小,相等. ...

  9. Linux显示按文件大小降序排列

    Linux显示按文件大小降序排列 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ ls -ls 总用量 56 12 -rw-r--r-- 1 youhaidon ...

随机推荐

  1. [Algorithm] Search for matching words

    Implement an autocomplete system. That is, given a query string s and a set of all possible query st ...

  2. [Android] Content provider, ContentResolver

    Content provider的作用: Content providers manage access to a structured set of data. They encapsulate t ...

  3. 使用(function() {}).call(this);包裹代码有什么好处,什么时候应该这样做?

    转自:http://segmentfault.com/q/1010000002519489 1.严格模式下函数调用的 this 并不会默认成为全局对象. 使用 func.call(this) 确保函数 ...

  4. PHP经典项目案例-(一)博客管理系统5

    本篇实现发表博客. 八.发表博客 (1).界面实现file.php <tr>      <td colSpan=3 valign="baseline" style ...

  5. Android开发之对话框高级应用

    Android开发之对话框高级应用 创建并显示一个对话框非常easy.可是假设想进行一些更高级点的操作,就须要一些技巧了.以下将和大家分享一下对话框使用的一些高级技巧. 1.改变对话框的显示位置: 大 ...

  6. PHP高级教程-高级过滤器

    PHP 高级过滤器 检测一个数字是否在一个范围内 以下实例使用了 filter_var() 函数来检测一个 INT 型的变量是否在 1 到 200 内: 实例 <?php $int = 122; ...

  7. hdu4059The Boss on Mars 容斥原理

    //求1到n之间与n互质的数的四次方的和 //segma(n^4) = (6n^5+15n^4+10n^3-n)/30 //对于(a/b)%mod能够转化为(a*inv(b))%mod //inv(b ...

  8. 设计模式之适配器模式(Adapter Pattern)C++实现

    适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁.这样的类型的设计模式属于结构型模式.它结合了两个独立接口的功能. 意图:将一个类的接口转换成客户希望的另外一个接口.适配器 ...

  9. 解决 Maven was cached in the local repository, resolution will not be reattempted until the update interv

    问题原因 Maven默认会使用本地缓存的库来编译工程,对于上次下载失败的库,maven会在~/.m2/repository/<group>/<artifact>/<ver ...

  10. 解决 jquery.form.js和springMVC上传 MultipartFile取不到信息

    前段页面: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnc ...