在Java中通过键命令HashMap的最佳方式是什么? [英] Best way to order an HashMap by key in Java?
问题描述
这是我第一次在Java中订购 HashMap
。我需要通过键来做到这一点,但在我的情况下,键是一个对象,所以我需要通过特定的字段进行排序。试图通过我自己的想法我已经考虑继续这个简单的代码:
private HashMap< SimpleDBField,String> ; sortTable(HashMap< SimpleDBField,String> row){
LinkedHashMap< SimpleDBField,String> orderedRow = new LinkedHashMap< SimpleDBField,String>();
for(int i = 1; i <= row.size(); i ++){
Iterator iterator = row.entrySet()。iterator();
while(iterator.hasNext()){
Map.Entry< SimpleDBField,String> entry =(Map.Entry< SimpleDBField,String>)iterator.next(); (entry.getKey()。getListPosition()== i){
orderedRow.put(entry.getKey(),entry.getValue());
if
休息;
}
}
}
return orderedRow;
$ b假设它起作用并且我不关心性能,它希望知道下一代代码可能是更好还是最重要的:为什么?
以下示例源代码:如何使用Java中的键和值对HashMap进行排序
public static< K extends Comparable,V extends Comparable>地图< K,V> sortByKeys(Map< K,V> map){
List< K> keys = new LinkedList< K>(map.keySet());
Collections.sort(keys);
地图< K,V> sortedMap = new LinkedHashMap< K,V>(); (K key:keys){
sortedMap.put(key,map.get(key));
}
return sortedMap;
$ b 如果两者都是错误的,我应该怎么做?
解决方案您无法控制 HashMap
的排序,如您所见。 LinkedHashMap
只是一个带有可预测的迭代次序的 HashMap
- 这是朝着正确方向迈出的一步,但它仍然是过于复杂的事情。 Java有一个内置的分类映射接口(使用不出意外的名字 SortedMap
)和一些实现,最流行的是 TreeMap
。使用它,让Java完成所有繁重的工作:
public static< K extends Comparable,V>地图< K,V> sortByKeys(Map< K,V> map){
返回新的TreeMap<>(map);
}
This is the first time that I have to order an HashMap
in Java. I need to do that by key but in my case the key is an object so I need to order by a specific field. Trying to figure it by my own I've considered to proceed with this simple scratch of code:
private HashMap<SimpleDBField, String> sortTable(HashMap<SimpleDBField, String> row){
LinkedHashMap<SimpleDBField, String> orderedRow = new LinkedHashMap<SimpleDBField, String>();
for(int i = 1; i <= row.size(); i ++){
Iterator iterator = row.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry<SimpleDBField, String> entry = (Map.Entry<SimpleDBField, String>) iterator.next();
if(entry.getKey().getListPosition()==i){
orderedRow.put(entry.getKey(), entry.getValue());
break;
}
}
}
return orderedRow;
}
Assuming that it works and I don't care about performance, before really use it, I wish to know if the next scratch of code could be better and most important: Why?
Example below source here: How to sort HashMap by key and value in Java
public static <K extends Comparable,V extends Comparable> Map<K,V> sortByKeys(Map<K,V> map){
List<K> keys = new LinkedList<K>(map.keySet());
Collections.sort(keys);
Map<K,V> sortedMap = new LinkedHashMap<K,V>();
for(K key: keys){
sortedMap.put(key, map.get(key));
}
return sortedMap;
}
If both are wrong, how should I do that?
解决方案 You cannot control a HashMap
's ordering, as you've seen. A LinkedHashMap
is just a HashMap
with a predictable iteration order - it's a step in the right direction, but it's still over-complicating things. Java has a built-in interface for sorted maps (with the unsurprising name SortedMap
), and a couple of implementation, the most popular one being a TreeMap
. Just use it and let Java do all the heavy lifting:
public static <K extends Comparable, V> Map<K,V> sortByKeys(Map<K,V> map) {
return new TreeMap<>(map);
}
这篇关于在Java中通过键命令HashMap的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!