传入一个Map<String,Long> 返回它按value排序后的结果
//传入一个Map<String,Long> 返回它按value排序后的结果 sort为正序还是倒序(-1倒序),size为要几条数据
private static Map<String, Long> sortMapByValues(Map<String, Long> aMap, int sort, int size) { Set<Map.Entry<String, Long>> mapEntries = aMap.entrySet(); List<Map.Entry<String, Long>> aList = new LinkedList<Map.Entry<String, Long>>(mapEntries); Collections.sort(aList, new Comparator<Map.Entry<String, Long>>() { @Override
public int compare(Map.Entry<String, Long> ele1,
Map.Entry<String, Long> ele2) {
if (sort < 0) {
return ele2.getValue().compareTo(ele1.getValue());
}
return ele1.getValue().compareTo(ele2.getValue());
}
});
// Storing the list into Linked HashMap to preserve the order of insertion.
Map<String, Long> aMap2 = new LinkedHashMap<String, Long>();
for (Map.Entry<String, Long> entry : aList) {
aMap2.put(entry.getKey(), entry.getValue());
if (aMap2.size() == size) {
break;
}
}
return aMap2;
}
传入一个Map<String,Long> 返回它按value排序后的结果的更多相关文章
- java中对List<Map<String,Object>>中的中文汉字排序
import java.text.Collator;import java.util.ArrayList;import java.util.Collections;import java.util.C ...
- C语言中函数中传入一个数组,并且返回一个数组
一.C语言可以很容易将一个数组传递给一个自定义函数,格式如下: main() { adb(float a[],int n); } float adb(float a[],int n) { …… ret ...
- Map.putAll方法——追加另一个Map对象到当前Map集合(转)
该方法用来追加另一个Map对象到当前Map集合对象,它会把另一个Map集合对象中的所有内容添加到当前Map集合对象. 语法 putAll(Map<? extends K,? extends V ...
- Map.putAll方法——追加另一个Map对象到当前Map集合
转: Map.putAll方法——追加另一个Map对象到当前Map集合(转) 该方法用来追加另一个Map对象到当前Map集合对象,它会把另一个Map集合对象中的所有内容添加到当前Map集合对象. 语法 ...
- Java中 如何把Object类型强转成Map<String, String>类型
首先你需要保证要转换的Object的实际类型是Map<String, String> 假设Object变量名为obj,强制转换(Map<String, String>)obj ...
- PAT 1039 Course List for Student (25分) 使用map<string, vector<int>>
题目 Zhejiang University has 40000 students and provides 2500 courses. Now given the student name list ...
- 对List<Map<String, Object>>集合排序
private void mySort(List<Map<String, Object>> list) { //list为待排序的集合,按SEQ字段排序 Comparator& ...
- JAVA传入一个字符串,返回一个字符串中的大写字母
/** * * @param 传入一个字符串 * @return 返回一个字符串中的大写字母 */ private static String str ...
- mybatis 获得一个map的返回集合
在使用mybatis 查询结果集,有时会有需求返回一个map比如表 id username 1 name1 2 name2 3 name3 希望的查询结果是一个map 并且以id为key 表为实体 ...
随机推荐
- 在vue中添加ico图标
准备:添加 ico图标在与index.html同级的目录 第一种方法: 在index.html中引入: <link rel="shortcuticon" type=" ...
- docker 踩坑日记The last packet sent successfully to the server was 0 milliseconds ago.
The last packet sent successfully to the server was 0 milliseconds ago. 今日遇到了这个坑,看似平白无奇. 首先,我定位到是数据库 ...
- 北航软件学院Java历届期末考题整理
文章目录 abstract static Thread finally package Exception I/O 子类和父类 关键字 标识符 垃圾收集 数据类型 环境配置 网路编程 initial ...
- RabbitMQ学习笔记(四、RabbitMQ队列)
目录: 消息路由失败了会怎样 备份交换器 TTL与DLX 如何实现延迟队列 RabbitMQ的RPC实现 持久化 事务 发送方确认机制 消息路由失败了会怎样: 在RabbitMQ中,如果消息路由失败了 ...
- acwing 7 混合背包
习题地址 https://www.acwing.com/problem/content/description/7/ 题目描述有 N 种物品和一个容量是 V 的背包. 物品一共有三类: 第一类物品只 ...
- acwing 55. 连续子数组的最大和
地址 https://www.acwing.com/problem/content/50/ 输入一个 非空 整型数组,数组里的数可能为正,也可能为负. 数组中一个或连续的多个整数组成一个子数组. 求 ...
- 算法问题实战策略 FENCE
地址 https://algospot.com/judge/problem/read/FENCE 开始考虑暴力遍历 #include <iostream> #include <str ...
- 一天两道PAT(2)1005,1006
今天的pat1006没什么好说的.就记录一下1005的状况.先上题目. 卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重 ...
- LeetCode解题笔记 - 4. Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- 设计模式-Proxy(结构型模式)
以下代码来源: 设计模式精解-GoF 23种设计模式解析附C++实现源码 //Proxy.h #pragma once class Subject { public: virtual ~Subject ...