根据嵌套在给定的hashmap内部的每个hashmap的大小,按顺序打印hashmap的内容
我在hashmap
内有一个hashmap
。我想根据内部hashmap
中的元素大小在hashmap
中打印元素。因此,元素数量最多的元素应首先打印。我是新来的hashmap,被卡住了。
这是hashmap
以及我如何打印它:
Map<String, Map<String, Integer>> states = new ConcurrentHashMap<String, Map<String, Integer>>();
for(Entry<String, Map<String, Integer>> entry : states.entrySet()) {
System.out.println("State:" + entry.getKey());
Map<String, Integer> tempMap = entry.getValue();
for(Entry<String, Integer> innerEntry : tempMap.entrySet()) {
System.out.println("City:" + innerEntry.getKey() + " Count:" + innerEntry.getValue());
}
System.out.println();
}
我当前获取的输出:
State:Texas
City:Austin Count:1
State:Hawaii
City:Honolulu Count:1
City:Kihei Count:1
City:Maui Count:1
State:California
City:Newport Beach Count:1
我需要的输出:
State:Hawaii
City:Honolulu Count:1
City:Kihei Count:1
City:Maui Count:1
State:Texas
City:Austin Count:1
State:california
City:Newport Beach Count:1
I have a HashMap
inside a HashMap
. I want to print the elements in the HashMap
according to the size of elements in the inner HashMap
. So the element with the highest number of elements should print first. I'm new to HashMaps and got stuck.
This is the HashMap
and how I'm printing it:
Map<String, Map<String, Integer>> states = new ConcurrentHashMap<String, Map<String, Integer>>();
for(Entry<String, Map<String, Integer>> entry : states.entrySet()) {
System.out.println("State:" + entry.getKey());
Map<String, Integer> tempMap = entry.getValue();
for(Entry<String, Integer> innerEntry : tempMap.entrySet()) {
System.out.println("City:" + innerEntry.getKey() + " Count:" + innerEntry.getValue());
}
System.out.println();
}
The output that I'm currently getting:
State:Texas
City:Austin Count:1
State:Hawaii
City:Honolulu Count:1
City:Kihei Count:1
City:Maui Count:1
State:California
City:Newport Beach Count:1
The output I need:
State:Hawaii
City:Honolulu Count:1
City:Kihei Count:1
City:Maui Count:1
State:Texas
City:Austin Count:1
State:california
City:Newport Beach Count:1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Hashmap
无法维护订单,因此您无法对其进行排序。您可以将地图内容转储到列表中,并使用自定义比较器对其进行排序。但是您缺少一些东西 - 构造数据的方式是错误的, state 和 cities state 是两个信息彼此密切相关。使用地图将它们结合在一起是滥用收藏品,这使您的代码僵化且无法实现。
使用对象的力量,
您的代码不会自动成为面向对象的因素,因为您使用的是面向对象的语言。
正确的方法是定义
状态
和city
作为类,并维护状态>状态
对象的列表,而不是处理嵌套 map 。这就是您可以在客户端代码中使用此类的方式:
输出:
HashMap
is not capable of maintaining the order, therefore you can't sort it.You can dump the map contents into a list and sort it using a custom comparator. But there's something that you're missing - the way you're structuring the data is wrong, state and cities that belong to that state are two pieces of information are closely related to each other. Using a map to combine them together is an abuse of collections, it makes your code rigid and unmaintainable.
Use the power of Objects
Your code doesn't become object-oriented automatically because of the fact that you're using an object-oriented language.
The correct approach would be to define
State
andCity
as classes and maintain a list ofState
objects instead of dealing with a nested map.That's how you can use this classes in the client code:
Output:
由于 concurrenthashmap 不保证订购,因此您必须对地图进行分类,然后将其排序,然后将其整理到A linkedhashmap
Since ConcurrentHashMap makes no guarantees about ordering, you have to sort your map be getting entrySet and sort them then re-put into a LinkedHashMap
通过点1中的映射键制作列表(size_list)。(密钥是不同状态的城市数)。以降序订购此列表
订购此列表,将size_list用作查找,并获取具有当前size_map hashmap的当前大小的状态列表。基本上使用size_list作为查找。
注意 - 这将增加您的开销,并且性能可能不佳。
Make a list (size_list) out of keys of map in point 1. (Keys are number of cities for different states). Order this list in descending order
While iterating your "states" Hashmap, use size_list as a lookup and fetch the list of states with the current size from size_map HashMap. Basically use size_list as a lookup.
Note - this will increase your overhead and performance might not be great.
您可以在打印出来之前使用Streams API进行排序:
You can use Streams API for sorting before print it out:
Hashmap不保证地图的顺序;特别是,它不能保证随着时间的流逝,订单将保持恒定。
改用LinkedHashMap,这看起来像:
现在,预期排序的映射为SortedMap
HashMap makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.
Using LinkedHashMap instead and that will look like:
Now, expected sorted map is sortedMap
尝试一下。
输出:
Try this.
output: