//传入一个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排序后的结果的更多相关文章

  1. java中对List<Map<String,Object>>中的中文汉字排序

    import java.text.Collator;import java.util.ArrayList;import java.util.Collections;import java.util.C ...

  2. C语言中函数中传入一个数组,并且返回一个数组

    一.C语言可以很容易将一个数组传递给一个自定义函数,格式如下: main() { adb(float a[],int n); } float adb(float a[],int n) { …… ret ...

  3. Map.putAll方法——追加另一个Map对象到当前Map集合(转)

    该方法用来追加另一个Map对象到当前Map集合对象,它会把另一个Map集合对象中的所有内容添加到当前Map集合对象. 语法  putAll(Map<? extends K,? extends V ...

  4. Map.putAll方法——追加另一个Map对象到当前Map集合

    转: Map.putAll方法——追加另一个Map对象到当前Map集合(转) 该方法用来追加另一个Map对象到当前Map集合对象,它会把另一个Map集合对象中的所有内容添加到当前Map集合对象. 语法 ...

  5. Java中 如何把Object类型强转成Map<String, String>类型

    首先你需要保证要转换的Object的实际类型是Map<String, String> 假设Object变量名为obj,强制转换(Map<String, String>)obj ...

  6. 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 ...

  7. 对List<Map<String, Object>>集合排序

    private void mySort(List<Map<String, Object>> list) { //list为待排序的集合,按SEQ字段排序 Comparator& ...

  8. JAVA传入一个字符串,返回一个字符串中的大写字母

    /**      *       * @param 传入一个字符串      * @return 返回一个字符串中的大写字母      */     private static String str ...

  9. mybatis 获得一个map的返回集合

    在使用mybatis 查询结果集,有时会有需求返回一个map比如表 id username 1  name1 2 name2 3 name3 希望的查询结果是一个map 并且以id为key  表为实体 ...

随机推荐

  1. JavaScript学习笔记-----NaN、isNan

    NaN  /  Number.NaN 全局属性 NaN 的值表示不是一个数字(Not-A-Number), NaN 属性的初始值就是 NaN,和 Number.NaN 的值一样. 在现代浏览器中(ES ...

  2. mysql登陆时出现ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0

    有4到5天没开mysql,这天晚上打=打开phpstudy,想进去mysql练习练习,结果丢给我这个 ERROR 2013 (HY000): Lost connection to MySQL serv ...

  3. 使用docker-compose部署springboot项目

    由于是单机测试,没有测试多主机的跨网络分布式请求. 项目是前后分离的,所以使用nginx作为前端服务器,后端是springboot则直接基于java8环境的容器上跑,cache用的redis,mysq ...

  4. 微信小程序官方文档中表单组建button部分有关function(type)中type的个人理解

    官方文档关于button组件的简介 xml页面挺容易理解,但js部分起初对整体写的形式都不太理解,随着逐渐阅读代码基本理解了 xml页面代码: <button type="defaul ...

  5. alluxio 安装记录及相关信息

    最近要尝试探究一下alluxio相关的知识,本博客进行对alluxio的安装过程进行备忘: 单例安装过程: https://docs.alluxio.io/os/user/stable/cn/cont ...

  6. Linux系统学习 八、SSH服务—SSH远程管理服务

    1.SSH简介 ssh(安全外壳协议)是Secure Shell的缩写,是建立在应用层和传输层基础上的安全协议.传输的时候是经过加密的,防止信息泄露,比telnet(明文传递)要安全很多. ftp安装 ...

  7. Codeforces Round #608 (Div. 2)

    传送门 A. Suits 签到. Code /* * Author: heyuhhh * Created Time: 2019/12/15 17:16:33 */ #include <iostr ...

  8. Python学习一、一个小例子

    一.题目: 对于一串氨基酸序列(由字母表前二十个大写字母组成),需要得到每一个氨基酸数目,然后输出到文件夹D:\test\frq.txt,要求用循环和字典实现. 氨基酸序列如下: ABCDEFGHIJ ...

  9. JMeter基础知识系列三

    JMeter测试结果字段的意义: Label:定义HTTP请求名称. Samples:表示这次测试中一共发出了多少个请求. Average:平均响应时长,当使用了Transaction Control ...

  10. WPF 后台获得 数据模板里的内容控件(DataTemplate)

    原文:WPF 后台获得 数据模板里的内容控件(DataTemplate) 假如      <Window.Resources> 里 有一个 Datatemplate 我想获得TextBlo ...