java - Sort a hash map which contains set of an objects
问题描述
I have a hashmap of a structure: Map<Integer,Set<class-object>> mymap = new HashMap<>()
In the map key is the integer and values are set of class objects
The class-object has the following variables in it
{
Integer id
String name
}
I want to sort the map in alphabetical order based on the class-object.name
variable.
How can we sort this map? Is it possible to sort such type of hashmap?
解决方案
你不能。HashMap 无法排序,因为它们是散列的。
要么使用 TreeMap 来保持键的顺序,要么将数据放在 List 中,这样你就可以按照你想要的任何顺序放置它们,包括通过调用 sort() 来排序。
推荐阅读
- regex - 用bash从字符串中提取整数
- node.js - 预检响应中的 Access-Control-Allow-Headers 不允许节点 Js 请求标头字段 Access-Control-Allow-Origin
- c - 冒泡排序一个单链表,它在每个节点中都包含一个字符串,在 C 中?
- c++ - 对象地址是否保证是其类型对齐的多个?
- laravel - 如何在 laravel 中的帖子上获取评论的名称
- java - 同步块无法正常工作
- java - 我正在使用带有 atlassian jira 的 `org.apache.poi` api,但它给出了错误
- node.js - 在 Windows 中通过 pm2 执行 npm 脚本的问题
- javascript - ngrx/store - 在角度 4 中重新加载页面后丢失数据
- javascript - Javascript 代码在 JSFiddle 中工作但不在我的页面中