Java 对HashMap 进行排序的几种场景
发布日期:2021-06-29 21:37:50
浏览次数:3
分类:技术文章
本文共 3859 字,大约阅读时间需要 12 分钟。
Java中HashMap不同于Array、ArrayList和LinkedLists,它不会维持插入元素的顺序。因此对HashMap排序的掌握很重要。HashMap排序有两个步骤:
- 1、通过map.entrySet()方法获取键值对;并将键值对传递给链表list;
- 2、自定义比较器来使用Collections.sort()方法排序链表。
关于map的遍历方法可参考
例如下面的按照value降序排列:Collections.sort(list, new Comparator>() { public int compare(Entry ele1, Entry ele2) { return ele1.getValue().compareTo(ele2.getValue()); } });
灵活使用Comparator接口可以实现HashMap的灵活排序:包括分别对键、值的升降序排序,优先按值、其次按键排序,或者优先按键、其次按值排序;以上实现只需重写compare方法。
1、对value降序排列
public int compare(Map.Entrye1,Map.Entry e2){ return e2.getValue().compareTo(e1.getValue()); }
2、对键KEY降序排列
public int compare(Map.Entrye1,Map.Entry e2){ return e2.getKey().compareTo(e1.getKey()); }
3、优先按值value降序、其次按键key升序排序
public int compare(Map.Entrye1,Map.Entry e2){ int re = e2.getValue().compareTo(e1.getValue()); if(re!=0){ return re;} else{ return e1.getKey().compareTo(e2.getKey());} }
4、优先按键key降序、其次按值value升序排序
public int compare(Map.Entrye1,Map.Entry e2){ int re = e2.getKey().compareTo(e1.getKey()); if(re!=0){ return re;} else{ return e1.getValue().compareTo(e2.getValue());} }
例题:
题意描述:输入一个只包含小写英文字母和数字的字符串,按照不同字符统计个数由多到少输出统计结果,如果统计的个数相同,则按照ASCII码由小到大排序输出。 本题含有多组样例输入 很简单的一个思路就是先对map的值排序,然后再按照键排序。import java.util.*;public class Main{ public static void main(String[] args) { Scanner cin=new Scanner(System.in); while(cin.hasNext()){ Mapmap = new HashMap (); String string=cin.next(); for(int i=0;i > list = new ArrayList<>(); list.addAll(map.entrySet()); Collections.sort(list,new Comparator >(){ public int compare(Map.Entry e1,Map.Entry e2){ int re = e2.getValue().compareTo(e1.getValue()); if(re!=0){ return re;} else{ return e1.getKey().compareTo(e2.getKey());} } }); for(Map.Entry map1:list){ System.out.print(map1.getKey()); } System.out.println(); } }}
当然,我们也可以利用普通类实现Comparable接口来对数据进行自定义排序
import java.util.*;public class Main { public static void main(String[] args) { Scanner cin=new Scanner(System.in); while(cin.hasNext()){ Mapmap = new HashMap (); String string=cin.next(); for(int i=0;i list=new ArrayList<>(); for(Map.Entry entry:map.entrySet()){ User user=new User(); user.setKey(entry.getKey()); user.setValue(entry.getValue()); list.add(user); user=null;//释放对象 } Collections.sort(list); for (User user:list){ System.out.print(user.getKey()); } System.out.println(); } } static class User implements Comparable { private Character key; private Integer value; public User() { } @Override public int compareTo(User user) { if(user.value==this.value) return this.key.compareTo(user.key); //value相同key升序 return user.value.compareTo(this.value); //value不同则value降序 } public Character getKey() { return key; } public void setKey(Character key) { this.key = key; } public Integer getValue() { return value; } public void setValue(Integer value) { this.value = value; } }}
转载地址:https://dh-butterfly.blog.csdn.net/article/details/110355513 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月22日 13时15分18秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
TypeScript 条件语句
2019-04-30
微信小程序 响应的数据绑定
2019-04-30
微信小程序框架
2019-04-30
微信小程序 场景值
2019-04-30
微信小程序 APP() 的那些事
2019-04-30
python 第一个程序 hello python
2019-04-30
python 注释
2019-04-30
python 变量类型
2019-04-30
Python int() 函数
2019-04-30
scrapy 框架安装
2019-04-30
scrapy 创建项目和新建爬虫
2019-04-30
scrapy 爬取起点中文网的作品分类
2019-04-30
浏览器与服务器建立TCP 连接后是否会在一个 HTTP 请求完成后断开?
2019-04-30
scrapy 爬取起点中文网首页的本周强推作品的详情介绍
2019-04-30
react 基础篇 1 hello React
2019-04-30
react 基础篇 2 创建虚拟DOM的方式
2019-04-30
react 基础 3 jsx语法规则
2019-04-30
react 基础篇 4 {表达式}
2019-04-30
react 基础篇 5 函数式组件
2019-04-30
HTTP 随记 4个小问题
2019-04-30