hashmap按value排序
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hashmap按value排序相关的知识,希望对你有一定的参考价值。
参考技术Aimport java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class SortHashMap
public static void main(String[] args)
//产生一个map并添加一些参数
Map<String, Integer> map = new HashMap<>();
map.put("ddd", 1);
map.put("aaa", 2);
map.put("bbb", 3);
map.put("ccc", 4);
List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet());
//将map的entryset放入list集合
// 对list进行排序,并通过Comparator传入自定义的排序规则
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>()
@Override
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2)
return o1.getValue() - o2.getValue(); //重写排序规则,小于0表示升序,大于0表示降序
);
//用迭代器对list中的键值对元素进行遍历
Iterator<Map.Entry<String, Integer>> iter = list.iterator();
while (iter.hasNext())
Map.Entry<String, Integer> item = iter.next();
String key = item.getKey();
int value = item.getValue();
System.out.println("键" + key + "值" + value);
如满意,请采纳
Java Map 按Key排序和按Value排序
package test; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.TreeMap; import java.util.Map.Entry; public class MapSortDemo { /** * @param args */ public static void main(String[] args) { Map<String, String> hMap = new HashMap<String, String>(); hMap.put("a", "3"); hMap.put("z", "2"); hMap.put("b", "6"); hMap.put("o", "9"); System.out.println("根据key升序排序"); Map<String, String> sortByKeyResultMap = sortMapByKey(hMap); //按Key进行排序 Iterator<Map.Entry<String, String>> sortByKeyEntries = sortByKeyResultMap.entrySet().iterator(); while (sortByKeyEntries.hasNext()) { Map.Entry<String, String> entry = sortByKeyEntries.next(); System.out.println("Key = " + entry.getKey() + "------->Value = " + entry.getValue()); } System.out.println("------------------------------"); System.out.println("根据value降序排序"); Map<String, String> sortByValueResultMap = sortMapByValue(hMap); //按Value进行排序 Iterator<Map.Entry<String, String>> sortByValueEntries = sortByValueResultMap.entrySet().iterator(); while (sortByValueEntries.hasNext()) { Map.Entry<String, String> entry = sortByValueEntries.next(); System.out.println("Key = " + entry.getKey() + "------->Value = " + entry.getValue()); } } /** * 使用 Map按key进行排序 * @param map * @return */ public static Map<String, String> sortMapByKey(Map<String, String> map) { if (map == null || map.isEmpty()) { return null; } // Map<String, String> sortMap = new TreeMap<String, String>(new MapKeyComparator()); Map<String, String> sortMap = new TreeMap<String, String>(new Comparator<String>() { public int compare(String obj1, String obj2) { return obj1.compareTo(obj2);//升序排序 } }); sortMap.putAll(map); return sortMap; } /** * 使用 Map按value进行排序 * @param map * @return */ public static Map<String, String> sortMapByValue(Map<String, String> map) { if (map == null || map.isEmpty()) { return null; } Map<String, String> sortedMap = new LinkedHashMap<String, String>(); List<Map.Entry<String, String>> entryList = new ArrayList<Map.Entry<String, String>>(map.entrySet()); // Collections.sort(entryList, new MapValueComparator()); Collections.sort( entryList, new Comparator<Map.Entry<String, String>>(){ public int compare(Entry<String, String> o1, Entry<String, String> o2) { return o2.getValue().compareTo(o1.getValue());// 降序排序 } } ); Iterator<Map.Entry<String, String>> iter = entryList.iterator(); Map.Entry<String, String> tmpEntry = null; while (iter.hasNext()) { tmpEntry = iter.next(); sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue()); } return sortedMap; } }
根据key升序排序 Key = a------->Value = 3 Key = b------->Value = 6 Key = o------->Value = 9 Key = z------->Value = 2 ------------------------------ 根据value降序排序 Key = o------->Value = 9 Key = b------->Value = 6 Key = a------->Value = 3 Key = z------->Value = 2
如果想把map排序代码分离出来就添加两个类,分别为
package test; import java.util.Comparator; public class MapKeyComparator implements Comparator<String> { @Override public int compare(String o1, String o2) { return o1.compareTo(o2); } }
package test; import java.util.Comparator; import java.util.Map; import java.util.Map.Entry; public class MapValueComparator implements Comparator<Map.Entry<String, String>> { @Override public int compare(Entry<String, String> o1, Entry<String, String> o2) { return o2.getValue().compareTo(o1.getValue()); } }
注意o1和o2的位置,它们在前还是在后就会变化升序和降序的顺序。
用key排序用以下代码
用value排序用以下代码
以上是关于hashmap按value排序的主要内容,如果未能解决你的问题,请参考以下文章