gpt4 book ai didi

java - 根据键和值对映射进行排序

转载 作者:行者123 更新时间:2023-12-02 07:15:39 25 4
gpt4 key购买 nike

我开发了下面的代码,我们可以在其中对 Hash Map 进行排序基于键以及值的基础。

主要逻辑是我们可以对Map进行排序,就是HashMapHashtable将 key 复制到 List比排序List通过使用Collections.sort()方法,这里您可以使用 ComparatorComparable根据您想要按自定义顺序还是自然顺序排序。一次List键已排序,我们可以创建另一个 Map,特别是 LinkedHashMap按排序顺序插入键。 LinkedHashMap将维护键插入的顺序,结果是基于键排序的 Map

我的问题是,如果您愿意,请告诉我其他更好的方法或你们可以建议的任何改进。

public class MapSortingExample {


public static void main(String args[]) {

//creating Hashtable for sorting
Map<String, Integer> olympic2012 = new HashMap<String, Integer>();

olympic2012.put("England", 3);
olympic2012.put("USA", 1);
olympic2012.put("China", 2);
olympic2012.put("Russia", 4);
//olympic2012.put("Australia", 4); //adding duplicate value

//printing hashtable without sorting
System.out.println("Unsorted Map in Java : " + olympic2012);

//sorting Map e.g. HashMap, Hashtable by keys in Java
Map<String, Integer> sorted = sortByKeys(olympic2012);
System.out.println("Sorted Map in Java by key: " + sorted);


//sorting Map like Hashtable and HashMap by values in Java
sorted = sortByValues(olympic2012);
System.out.println("Sorted Map in Java by values: " + sorted);


//Sorting Map in Java by keys using TreeMap
Map<String, Integer> sortedMapByKeys = new TreeMap<String,Integer>();
sortedMapByKeys.putAll(olympic2012);
System.out.println("Sorted Map in Java by key using TreeMap : " + sortedMapByKeys);


//Sorting Map by keys in Java using Google Collections (Guava)
//Main benefit is you can specify any ordering like natural or toString or arbitrary
Map<String, Integer> sortingUsingGuava = Maps.newTreeMap(Ordering.natural());
sortingUsingGuava.putAll(olympic2012);
System.out.println("Example to sort Map in Java using Guava : " + sortingUsingGuava);



}

/*
* Paramterized method to sort Map e.g. HashMap or Hashtable in Java
* throw NullPointerException if Map contains null key
*/
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);

//LinkedHashMap will keep the keys in the order they are inserted
//which is currently sorted on natural ordering
Map<K,V> sortedMap = new LinkedHashMap<K,V>();
for(K key: keys){
sortedMap.put(key, map.get(key));
}

return sortedMap;
}

/*
* Java method to sort Map in Java by value e.g. HashMap or Hashtable
* throw NullPointerException if Map contains null values
* It also sort values even if they are duplicates
*/
public static <K extends Comparable,V extends Comparable> Map<K,V> sortByValues(Map<K,V> map){
List<Map.Entry<K,V>> entries = new LinkedList<Map.Entry<K,V>>(map.entrySet());

Collections.sort(entries, new Comparator<Map.Entry<K,V>>() {

@Override
public int compare(Entry<K, V> o1, Entry<K, V> o2) {
return o1.getValue().compareTo(o2.getValue());
}
});

//LinkedHashMap will keep the keys in the order they are inserted
//which is currently sorted on natural ordering
Map<K,V> sortedMap = new LinkedHashMap<K,V>();

for(Map.Entry<K,V> entry: entries){
sortedMap.put(entry.getKey(), entry.getValue());
}

return sortedMap;
}

}

输出将是..

Unsorted Map in Java : {USA=1, England=3, Russia=4, China=2}
Sorted Map in Java by key: {China=2, England=3, Russia=4, USA=1}
Sorted Map in Java by values: {USA=1, China=2, England=3, Russia=4}
Sorted Map in Java by key using TreeMap : {China=2, England=3, Russia=4, USA=1}
Example to sort Map in Java using Guava : {China=2, England=3, Russia=4, USA=1}

最佳答案

更好的方法是使用 TreeMap 和给定的比较器,而不是“手动”排序并将结果放入 LinkedHashMap

关于java - 根据键和值对映射进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14964040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com