eg_7
1. 给定Map,根据Map中的值从大到小排列
package com.studentmanager.www; import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
public class Test12 {
public static void main(String[] args) {
Map<Integer,Integer> map = new TreeMap<Integer,Integer>();
map.put(5, 3);
map.put(6, 4);
map.put(2, 2);
map.put(1, 7);
//这里将map.entrySet()转换成list
List<Map.Entry<Integer,Integer>> list = new ArrayList<Map.Entry<Integer,Integer>>(map.entrySet());
//然后通过比较器来实现排序
Collections.sort(list,new Comparator<Map.Entry<Integer,Integer>>() {
public int compare(Entry<Integer,Integer> o1,
Entry<Integer,Integer> o2) {
//升序排序
// return o1.getValue().compareTo(o2.getValue());
//降序排序
return o2.getValue().compareTo(o1.getValue());
}
});
for(Map.Entry<Integer,Integer> mapping:list){
System.out.println(mapping.getKey()+":"+mapping.getValue());
}
}
}
运行结果:
1:7
6:4
5:3
2:2
eg_7的更多相关文章
随机推荐
- Hexo之NexT配置过程中遇到的问题
写在前面 从搭建到布置到GitHub花费时间大概用了五个小时,中间删除repo N次,localhost运行正常,git上出现过404页面.若干次README.md页面,本文将简略介绍搭建过程,主要内 ...
- SVG 动画(animate、animateTransform、animateMotion)
原文:https://blog.csdn.net/chy555chy/article/details/53535581 参考 MDN开发文档 https://developer.mozilla.org ...
- 【转】深入学习Redis(1):Redis内存模型
原文:https://www.cnblogs.com/kismetv/p/8654978.html 前言 Redis是目前最火爆的内存数据库之一,通过在内存中读写数据,大大提高了读写速度,可以说Red ...
- 1 nodejs
重点 :
- 【CF617D】Roads in Yusland
[CF617D]Roads in Yusland 题面 蒯的洛谷的 题解 我们现在已经转化好了题目了,戳这里 那么我们考虑怎么求这个东西,我们先判断一下是否所有的边都能被覆盖,不行的话输出\(-1\) ...
- centos中如何添加环境变量
在Linux CentOS系统上安装完php和MySQL后,为了使用方便,需要将php和mysql命令加到系统命令中,如果在没有添加到环境变量之前,执行“php -v”命令查看当前php版本信息时时, ...
- P4774 [NOI2018]屠龙勇士
P4774 [NOI2018]屠龙勇士 先平衡树跑出打每条龙的atk t[] 然后每条龙有\(xt \equiv a[i](\text{mod }p[i])\) 就是\(xt+kp[i]=a[i]\) ...
- 在使用Reference Source调试.Net 源代码时如何取消optimizations(代码优化)-翻译
在使用PDB调试XAF时,发现好多变量都看不到.都被优化掉了. 下面的方法可以解决. 当你在使用Reference Source functionality in VS 2008 调试.Net 的源代 ...
- Direct2D处理几何图形之间的碰撞检测(上)
转载请注明出处:http://www.cnblogs.com/Ray1024 一.概述 Direct2D中支持以下几种类型的几何图形: a.简单几何图形(Simple Geometry):矩形.圆角矩 ...
- selenium无法正常运行 Chrome浏览器,cannot find Chrome binary的问题
有些同学在运行selenium-chrome时会遇到这个问题, System.setProperty("webdriver.chrome.driver","files/c ...