HashMap排序题
HashMap排序题
题目
已知一个 HashMap<Integer,User>集合, User 有 name(String)和 age(int)属性。
请写一个方法实现对HashMap 的排序功能,该方法接收 HashMap<Integer,User>为形参,返回类型为 HashMap<Integer,User>,
要求对 HashMap 中的 User 的 age 倒序进行排序。排序时 key=value 键值对不得拆散。
注意:要做出这道题必须对集合的体系结构非常的熟悉。
HashMap本身就是不可排序的,但是该题偏偏让HashMap排序,那我们就得想在API中有没有这样的 Map 结构是有序的,
我们不难发现其中LinkedHashMap就具有这样的结构,是链表结构有序的,更可喜的是他是 HashMap的子类,
我们返回LinkedHashMap<Integer,User>即可,还符合面向接口编程的思想。
但凡是对集合的操作,我们应该保持一个原则就是能用JDK中的API就用JDK中的 API,
比如排序算法我们不应该去用冒泡或者选择,而是首先想到用 Collections 集合工具类。
实现代码
```
package com;
import java.util.*;
public class Test {
public static void main(String[] args) {
HashMap<Integer, User> users = new HashMap<>();
users.put(1,new User("张三",25));
users.put(3,new User("李四",22));
users.put(2, new User("王五", 28));
System.out.println(users);//{1=User{name='张三', age=25}, 2=User{name='王五', age=28}, 3=User{name='李四', age=22}}
HashMap<Integer, User> sortHashMap = sortHashMap(users);
System.out.println(sortHashMap);//{2=User{name='王五', age=28}, 1=User{name='张三', age=25}, 3=User{name='李四', age=22}}
}
public static HashMap<Integer,User> sortHashMap(HashMap<Integer,User> map){
//拿到map集合中的键值对集合
Set<Map.Entry<Integer, User>> entrySet = map.entrySet();
//将set集合转换为list集合:为了使用工具类排序
List<Map.Entry<Integer, User>> list = new ArrayList<>(entrySet);
//使用collections集合工具类对list进行排序:重写排序规则new Comparator
Collections.sort(list, new Comparator<Map.Entry<Integer, User>>() {
@Override
public int compare(Map.Entry<Integer, User> o1, Map.Entry<Integer, User> o2) {
//根据user的age的倒叙排序
return o2.getValue().getAge()-o1.getValue().getAge();
}
});
//创建一个新的有序的HashMap子类的集合
LinkedHashMap<Integer, User> linkedHashMap = new LinkedHashMap<>();
//将list中数据存储在linkedHashMap中
for (Map.Entry<Integer,User> entry :list){
linkedHashMap.put(entry.getKey(),entry.getValue());
}
return linkedHashMap;
}
}
```
HashMap排序题的更多相关文章
- HashMap 排序
本文章,摘抄自:2018黑马程序最新面试题汇总 已知一个 HashMap<Integer,User>集合, User 有 name(String)和 age(int)属性.请写一个方法实现 ...
- HashMap排序的问题
那么已知一个HashMap<Integer,User>集合, User有name(String)和 age(int)属性.请写一个方法实现对HashMap 的排序功能,该方法接收 Hash ...
- PAT甲级 排序题_C++题解
排序题 PAT (Advanced Level) Practice 排序题 目录 <算法笔记> 6.9.6 sort()用法 <算法笔记> 4.1 排序题步骤 1012 The ...
- 【算法学习记录-排序题】【PAT A1012】The Best Rank
To evaluate the performance of our first year CS majored students, we consider their grades of three ...
- 给HashMap排序,使之成为有序Map
如何给HashMap中的值排序?这个问题很多人都遇到过,很常见的一个方案是使用LinkedHashMap,因为LinkedHashMap可以记住元素放入的顺序,可以认为是真正的“有序”(想让HashM ...
- Map HashMap 排序 迭代循环 修改值
HashMap dgzhMap = Dict.getDict("dgzh"); Iterator it_d = dgzhMap.entrySet().iterator(); whi ...
- Java零基础手把手系列:HashMap排序方法一网打尽
HashMap的排序在一开始学习Java的时候,比较容易晕,今天总结了一些常见的方法,一网打尽.HashMap的排序入门,看这篇文章就够了. 1. 概述 本文排序HashMap的键(key)和值(va ...
- 【算法学习记录-排序题】【PAT A1016】Phone Bills
A long-distance telephone company charges its customers by the following rules: Making a long-distan ...
- leetcode之链表排序题
原文链接:点击打开链接 原题是这样的: Given a linked list and a value x, partition it such that all nodes less than x ...
- Java中HashMap排序
注: 转载于 http://www.cnblogs.com/xingyun/archive/2012/12/09/2809962.html package com.holdobject; import ...
随机推荐
- 数据结构 传统链表实现与Linux内核链表
头文件: #pragma once #include<stdlib.h> //链表结点 struct LinkNode{ void *data; struct LinkNode *next ...
- Redis RDB 与AOF
参考书籍<Redis设计与实现> 一丶为什么redis需要持久化 redis 作为一个内存数据库,如果不想办法将存储在内存中的数据,保存到磁盘中,那么一旦服务器进程退出,那么redis数据 ...
- SQLSERVER 的复合索引和包含索引到底有啥区别?
一:背景 1. 讲故事 在 SQLSERVER 中有非常多的索引,比如:聚集索引,非聚集索引,唯一索引,复合索引,Include索引,交叉索引,连接索引,奇葩索引等等,当索引多了之后很容易傻傻的分不清 ...
- Spring Boot 3.0横空出世,快来看看是不是该升级了
目录 简介 对JAVA17和JAVA19的支持 record Text Blocks Switch Expressions instanceof模式匹配 Sealed Classes and Inte ...
- [C++]const_cast,dynamic_cast,reinterpret_cast,static_cast转型
C++四种新式转型: const_cast(expression) dynamic_cast(expression) reinterpret_cast(expression) static_cast( ...
- 前端Linux部署命令与流程记录
以前写过一篇在Linux上从零开始部署前后端分离的Vue+Spring boot项目,但那时候是部署自己的个人项目,磕磕绊绊地把问题解决了,后来在公司有了几次应用到实际生产环境的经验,发现还有很多可以 ...
- odoo中的字段创建后,不可以编辑
- OpenMP 线程同步 Construct 实现原理以及源码分析(下)
OpenMP 线程同步 Construct 实现原理以及源码分析(下) 前言 在上面文章当中我们主要分析了 flush, critical, master 这三个 construct 的实现原理.在本 ...
- Collection集合常用功能-Iterator接口介绍
Collection集合常用功能 Collection是所有单列集合的父接口,因此在Collection中定义了单列集合(List和Set)通用的一些方法,这些方法可用于操作所有的单列集合.方法如下︰ ...
- ES简介
https://www.bbsmax.com/A/E35pW7LEJv/ 1 什么是ES ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式的全文搜索引擎,其对外服务是基 ...