java按照map的value排序
来源:互联网 发布:安装win10 linux的pe 编辑:程序博客网 时间:2024/05/17 01:02
java的TreeMap可以排序,只可惜是按照key来排序的,或者重写其他Map的排序算法也都是按照key来排序的,下面贴出来一个按照value排序的算法:
public class SortMap { public static void main(String[] args) throws Exception { // TODO code application logic here Map<String, Integer> myMap = new LinkedHashMap(); myMap.put("1", 1); myMap.put("2", 4); myMap.put("3", 3); myMap.put("4", 9); myMap.put("5", 6); myMap.put("6", 2); printMap(myMap); myMap = sortMap(myMap); printMap(myMap); } private static void printMap(Map map){ System.out.println("===================mapStart=================="); Iterator it = map.entrySet().iterator(); while(it.hasNext()){ Map.Entry entry = (Map.Entry) it.next(); System.out.println(entry.getKey() + ":" + entry.getValue()); } System.out.println("===================mapEnd=================="); } public static Map sortMap(Map oldMap) { ArrayList<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(oldMap.entrySet()); Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() { @Override public int compare(Entry<java.lang.String, Integer> arg0, Entry<java.lang.String, Integer> arg1) { return arg0.getValue() - arg1.getValue(); } }); Map newMap = new LinkedHashMap(); for (int i = 0; i < list.size(); i++) { newMap.put(list.get(i).getKey(), list.get(i).getValue()); } return newMap; }}
- java按照map的value排序
- java按照map的value排序
- java按照map的value排序
- java按照map的value排序
- java按照map的value排序
- Java Map按照Value 排序
- java map按照value排序
- map 按照value排序
- Map按照Value值排序
- Java中Map按照Value值进行排序
- Java Map集合按照key和value排序之法
- Map按照Key排序和Map按照Value排序
- Map中按照value的大小进行排序
- Map的插入操作和按照value排序
- Map的插入操作和按照value排序
- 关联容器map按照value排序或者按照key排序
- Map集合按照Value排序(Map value sort)
- C++ map容器 按照value值排序
- 第六章 模型-视图-控制器应用程序设计
- 电脑开机无显示故障的排除方法(查看有没有起鼓的电容)
- java面试必考
- signal(SIGPIPE, SIG_IGN)
- 模式识别中运用增L减R法进行特征的选取
- java按照map的value排序
- mysql 字段类型、基本命令
- Mapreduce中的RCFile输出RCFileOutputFormat实现及其应用
- std:auto_ptr
- 数据结构(C语言)读书笔记9:串的基本概念
- 计算一个字符串表示的四则运算表达式
- java基础深入理解
- android android:windowSoftInputMode相关属性
- 算法导论 最长公共子序列