一、实现的思想

  1. 将HashMap中的元素按照Entry<Key,Value>类型存入到LinkedList集合中。
  2. 实现自定义排序,对LinkedList集合排序。
  3. LinkedList集合的元素存入到HashMap中,返回排序好的结果

二、代码实现

    /**
*
* @param map HashMap<String, Integer> 按照值进行排序
* @return:返回排序后的Map
*/
public static HashMap<String, Integer> hashMapSort(HashMap<String, Integer> map){
//1、按顺序保存map中的元素,使用LinkedList类型
List<Entry<String, Integer>> keyList = new LinkedList<Entry<String, Integer>>(map.entrySet());
//2、按照自定义的规则排序
Collections.sort(keyList, new Comparator<Entry<String, Integer>>() {
@Override
public int compare(Entry<String, Integer> o1,
Entry<String, Integer> o2) {
if(o2.getValue().compareTo(o1.getValue())>0){
return 1;
}else if(o2.getValue().compareTo(o1.getValue())<0){
return -1;
} else {
return 0;
}
} });
//3、将LinkedList按照排序好的结果,存入到HashMap中
HashMap<String,Integer> result=new LinkedHashMap<>();
for(Entry<String, Integer> entry:keyList){
result.put(entry.getKey(),entry.getValue());
}
return result;
}

HashMap按照value排序的实现的更多相关文章

  1. 如何对HashMap按键值排序

    Java中HashMap是一种用于存储“键”和“值”信息对的数据结构.不同于Array.ArrayList和LinkedLists,它不会维持插入元素的顺序. 因此,在键或值的基础上排序HashMap ...

  2. Treemap 有序的hashmap。用于排序

    TreeMap:有固定顺序的hashmap.在需要排序的Map时候才用TreeMap. Map.在数组中我们是通过数组下标来对其内容索引的,键值对. HashMap HashMap 用哈希码快速定位一 ...

  3. 实现对HashMap的value排序

    问题:如何对HashMap中的value值进行排序 关键点:1.取HashMap的Map.Entry,放入List2.利用Collections.sort(List, Comparator<? ...

  4. Java面试题:如何对HashMap按键值排序

    Java中HashMap是一种用于存储“键”和“值”信息对的数据结构.不同于Array.ArrayList和LinkedLists,它不会维持插入元素的顺序. 因此,在键或值的基础上排序HashMap ...

  5. Java HashMap 默认排序

    先看一段Java代码. package com.m58.test; import java.text.ParseException; import java.text.SimpleDateFormat ...

  6. 关于hashmap的排序

    刚学java不久 之前在学习hashmap的时候 无意间发现,诶?怎么结果是排序的,然后重新输入了好多次,握草,原来java 1.8都实现了hashmap的排序 天真的我没有去网上查,没有去想java ...

  7. 如何高效的遍历HashMap 以及对key 进行排序

    Map<Integer ,Object> map = new HashMap<Integer,Object>(); for(int i = 0; i<=100;i++){ ...

  8. java 中对hashmap进行排序

    public class HashMapSort { public static void main(String[] args) { HashMap<Integer, Student> ...

  9. [Java集合] 彻底搞懂HashMap,HashTable,ConcurrentHashMap之关联.

    注: 今天看到的一篇讲hashMap,hashTable,concurrentHashMap很透彻的一篇文章, 感谢原作者的分享. 原文地址: http://blog.csdn.net/zhanger ...

随机推荐

  1. asp.net webform设计思路的思考

    我使用asp.net的webform框架进行web应用程序的开发已经差不多四年了,在整个开发生涯中,也使用过一年asp.net的mvc框架.因为网上经常有讨论webform框架和mvc框架的优劣,所以 ...

  2. Minify or format javascript file by web or notepad++

    Notepad++ plugin manager install 'JSTOOL' http://tool.oschina.net/codeformat/js https://www.cnblogs. ...

  3. Javascript中实现继承的方式

    js中实现继承和传统的面向对象语言中有所不同:传统的面向对象语言的继承由类来实现,而在js中,是通过构造原型来实现的,原型与如下几个术语有关: ①构造函数:在构造函数内部拥有一个prototype属性 ...

  4. mysql 和php 保留2位小数

    一般交易中保留的数字的小数位数为2位(即最小单位为 1分钱[0.01元]) 数据库设计中预金钱有关或要求精准度要高的用 decimal(n,m) 表示,n表示保留的数字长度,保留的小数位数,如deci ...

  5. Jquery 组 表单select交互选项

    <!DOCTYPE html><html lang="zh-cn"><head> <meta charset="utf-8&qu ...

  6. WPF将数据库和GridView绑定并更改GridView模板

    首先来看一下如何使用GridView,在前台的话代码如下:这里仅仅举出一个例子,GridView是作为子项嵌套在ListView中的,这里的数据源是通过绑定的方式来绑定到GridView中的. < ...

  7. rabbitmq代码配置

    package com.qukoucai.test; import com.rabbitmq.client.AMQP;import com.rabbitmq.client.AMQP.BasicProp ...

  8. iOS开发中@property的属性weak nonatomic strong readonly等

    请看  https://www.cnblogs.com/liubeimeng/p/4244686.html

  9. Django-website 程序案例系列-9 分页

    分页例子程序: LIST = [] #全局列表 for i in range(103): #1:100的列表 LIST.append(i) def user_list(request): curren ...

  10. BZOJ5311 贞鱼(动态规划+wqs二分+决策单调性)

    大胆猜想答案随k变化是凸函数,且有决策单调性即可.去粘了份fread快读板子才过. #include<iostream> #include<cstdio> #include&l ...