Map的序列的排序
来源:互联网 发布:亚投行结局是笑话知乎 编辑:程序博客网 时间:2024/06/08 20:31
package com.jjyy.basic;import java.util.Collections;import java.util.Comparator;import java.util.HashMap;import java.util.LinkedHashMap;import java.util.LinkedList;import java.util.List;import java.util.Map;import java.util.Map.Entry;import java.util.Set;import org.junit.Before;import org.junit.Test;/** * map的排序 * @author jiangyu 2015年9月18日 * */public class SortMapDemo {private Map<String, Integer> map = new HashMap<String, Integer>();/** * Map 排序操作 * @param aMap */private void sortMapByValues(Map<String, Integer> aMap) {Set<Entry<String, Integer>> mapEntities = aMap.entrySet();System.out.println("Values and Keys before sorting ...");System.out.println("V"+" - "+"K");for (Entry<String, Integer> entry : mapEntities) {System.out.println(entry.getValue()+" - "+entry.getKey());}// used linked list to sort ,because insertion of elements in// linked list is faster than an array listList<Entry<String, Integer>> aList = new LinkedList<Map.Entry<String,Integer>>(mapEntities);//sorting the listCollections.sort(aList, new Comparator<Entry<String, Integer>>() {@Overridepublic int compare(Entry<String, Integer> ele1, Entry<String, Integer> ele2) {return ele1.getValue().compareTo(ele2.getValue());}});//Sorting the list into Linked HashMap to preserve the order of insertion Map<String, Integer> aMap2 = new LinkedHashMap<String, Integer>();for (Entry<String, Integer> entry : aList) {aMap2.put(entry.getKey(), entry.getValue());}//Printing values after sorting of mapSystem.out.println("V"+" - "+"K");for (Entry<String, Integer> entry : aMap2.entrySet()) {System.out.println(entry.getValue() +" - "+entry.getKey());}}@Beforepublic void initValue(){map.put("Six", 6);map.put("One", 1);map.put("Three", 3);map.put("Seven", 7);map.put("Four", 4);map.put("Two", 2);map.put("Five", 5);}@Testpublic void testMapSort(){sortMapByValues(map);}}
结果:
Values and Keys before sorting ...V - K6 - Six7 - Seven5 - Five3 - Three1 - One2 - Two4 - FourV - K1 - One2 - Two3 - Three4 - Four5 - Five6 - Six7 - Seven
0 0
- Map的序列的排序
- 一个排序的Map
- 自定义map的排序
- Std::map的排序
- Java Map的排序
- Map的值排序
- map的排序
- map集合的排序
- map的排序总结
- map/multimap的排序
- 对Map的排序
- Map的排序Demo
- java8 的 map 排序
- 序列的排序
- stl序列容器(map和set)的仿函数排序
- 关于Map的排序问题
- map和set的排序
- std::map排序的原理
- ActiveMQ简单demo
- Intent与IntentFilter
- #if defined #ifdef #ifndef 的区别
- 使用axis开发webservice服务java.net.ConnectException: Connection refused: connect异常的解决办法
- MFC中静态文本框(CStatic)响应鼠标单击事件
- Map的序列的排序
- Python+Matplotlib+PyQt4做的简单数据预测工具
- spring mvc 自定义HandlerMethodArgumentResolver
- 数据挖掘入门笔记(一)--认识数据
- Parallax Mapping
- V8 之旅: 垃圾回收器
- hdu 5445 Food Problem 分组背包,二进制拆分
- hdu2196
- Spring注解