转载地址

已知一个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,对的,就是他,他是Map结构,也是链表结构,有序的,更可喜的是他是HashMap的子类,我们返回LinkedHashMap<Integer,User>即可,还符合面向接口(父类编程的思想)。
但凡是对集合的操作,我们应该保持一个原则就是能用JDK中的API就有JDK中的API,比如排序算法我们不应该去用冒泡或者选择,而是首先想到用Collections集合工具类。

public static void main(String[] args) {
HashMap<Integer, User> users = new HashMap<Integer, User>();
users.put(1, new User("张三", 25));
users.put(3, new User("李四", 22));
users.put(2, new User("王五", 28));
users.put(4, new User("赵六", 18)); System.out.println( "排序前:" + users); HashMap<Integer, User> sorHashMap = sortHashMap(users);
System.out.println("排序后:" + sorHashMap);
}
public static HashMap<Integer, User> sortHashMap( HashMap<Integer, User> map){
// 首先拿到map的键值对集合
Set<Entry<Integer, User>> entrySet = map.entrySet();
// 将set集合转为List集合,为什么,为了使用工具类的排序方法
List<Entry<Integer, User>> list = new ArrayList<Entry<Integer, User>>(entrySet);
// 使用Collections集合工具类对list进行排序,排序规则使用匿名内部类来实现
Collections.sort(list, new Comparator<Entry<Integer, User>>() {
@Override
public int compare(Entry<Integer, User> o1, Entry<Integer, User> o2) {
//按照要求根据User的age的倒序进行排
return o2.getValue().getAge() - o1.getValue().getAge(); //按照user的age倒序排列
}
});
//创建一个新的有序的HashMap子类的集合
LinkedHashMap<Integer, User> linkedHashMap = new LinkedHashMap<Integer, User>();
//将List中的数据存储在LinkedHashMap中
for (Entry<Integer,User> entry : list) {
linkedHashMap.put(entry.getKey(), entry.getValue());
}
//返回结果
return linkedHashMap;
} public class User {
String name;
Integer age; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public User(){}
public User(String name, Integer age) {
super();
this.name = name;
this.age = age;
};
}
输出结果:

排序前:{1=User [name=张三, age=25], 2=User [name=王五, age=28], 3=User [name=李四, age=22], 4=User [name=赵六, age=18]}
排序后:{4=User [name=赵六, age=18], 3=User [name=李四, age=22], 1=User [name=张三, age=25], 2=User [name=王五, age=28]}
原文:https://blog.csdn.net/infinitezhen/article/details/51317499 

最新文章

  1. Android Gson解析
  2. [javaSE] 注解-自定义注解
  3. Eclipse关闭XML文件验证的方法
  4. 十.oc内存管理
  5. 【读书笔记】iOS网络-Web Service协议与风格
  6. PyCharm 远程连接linux中Python 运行pyspark
  7. ROSE User Case View
  8. HDU-5785 Interesting(Manacher算法+区间处理)
  9. CSS 中定位的使用
  10. [转载]C# 判断字符是否中文还是英文
  11. UISearchBar 光标不出现的问题
  12. 老李推荐:第8章6节《MonkeyRunner源码剖析》MonkeyRunner启动运行过程-启动Monkey 4
  13. 什么是命名空间?php命名空间的基本应用分享
  14. Linux下ip配置与网络重启
  15. ASCII编码查看
  16. linux重启Oracle服务
  17. MySQL基础知识3
  18. laravel iis搭建
  19. hdoj2604 Queuing(矩阵快速幂)
  20. Windows Forms编程实战学习:第二章 欢迎使用Visual Studio

热门文章

  1. log4j不输出日志的解决方案
  2. 使用DDMS查看设备内的文件系统
  3. 读书笔记 C#委托的BeginInvoke、EndInvoke之浅析
  4. C数据结构 : 线性表 与 链表
  5. 第六节 静态的(static)和单例模式
  6. 2017 秦皇岛CCPC Balloon Robot (ZOJ 3981)
  7. HTML5中的data-*属性和jQuery中的.data()方法使用
  8. Linux:NFS配置
  9. python 元组攻略
  10. Wrapper