Map排序(按key/按value)
package com.abc.test; import java.util.ArrayList;
import java.util.Arrays;
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.Scanner;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.Map.Entry; /**
* 可能会遇到这样的情况,我可能要对Map<key,value>的集合进行排序,而这种排序又分为两种情况,你可能按key值排序;
* 另外你也可能会遇到按value值进行排序的情况。
* 大家都知道,默认的情况下,TreeMap:是按key升序,进行排序的;LinkedHashMap:是按加入顺序进行排序的
* ;HashMap:内部数值的顺序并不是以存放的先后顺序为主
* ,而是以hash值的顺序为主,其次才是存放的先后顺序。在这里我们只讨论如何实现HashMap的排序。
*
*/
@SuppressWarnings("unchecked")
public class MapSortTest { public static void main(String[] args) {
//初始化map
Map<String, Integer> map=getMapInstance();
printMap(map); //选择操作
Scanner input=new Scanner(System.in);
int num=input.nextInt();
switch (num) {
case 0:
System.exit(0);
break;
case 1:
Map<String, Integer> sortMaps = sortMapByKey(map);
printMap(sortMaps);
break;
case 2:
sortMapByValue(map);
break;
case 3:
mapSortByKey(map);
break;
case 4:
mapSortByValue(map);
break;
default:
System.out.println("error input!");
break;
} } public static Map sortMapByKey(Map map) {
Map<Object, Object> mapVK = new TreeMap<Object, Object>(
new Comparator<Object>() {
public int compare(Object obj1, Object obj2) {
String v1 = (String) obj1;
String v2 = (String) obj2;
int s = v2.compareTo(v1);
return s;
}
});
Set col = map.keySet();
Iterator iter = col.iterator();
while (iter.hasNext()) {
String key = (String) iter.next();
Integer value = (Integer) map.get(key);
mapVK.put(key, value);
}
return mapVK;
} public static void sortMapByValue(Map maps) {
List<Map.Entry<String, Integer>> info = new ArrayList<Map.Entry<String, Integer>>(maps.entrySet());
Collections.sort(info, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> obj1,Map.Entry<String, Integer> obj2) {
return obj2.getValue() - obj1.getValue();
}
});
for (int j = 0; j < info.size(); j++) {
System.out.println(info.get(j).getKey() + "------->"+ info.get(j).getValue());
}
} // ====================================================================================
private static SortedMap<String, Integer> mapSortByKey(Map<String, Integer> unsort_map) {
TreeMap<String, Integer> result = new TreeMap<String, Integer>();
Object[] unsort_key = unsort_map.keySet().toArray();
Arrays.sort(unsort_key);
for (int i = 0; i < unsort_key.length; i++) {
result.put(unsort_key[i].toString(), unsort_map.get(unsort_key[i]));
}
return result.tailMap(result.firstKey());
} public static void mapSortByValue(Map map) {
List arrayList = new ArrayList(map.entrySet());
Collections.sort(arrayList,new Comparator(){
@Override
public int compare(Object o1, Object o2) {
Map.Entry obj1 = (Map.Entry)o1;
Map.Entry obj2 = (Map.Entry)o2;
return obj1.getValue().toString().compareTo(obj2.getValue().toString());
}
});
for(Iterator it = arrayList.iterator();it.hasNext();){
Map.Entry entry = (Map.Entry)it.next();
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
public static void abc(Map map){
TreeMap treemap = new TreeMap(map);
}
public static void abcd() {
Map<String, Integer> keyfreqs = new HashMap<String, Integer>();
ArrayList<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(keyfreqs.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1,Map.Entry<String, Integer> o2) {
return (o2.getValue() - o1.getValue());
}
});
for (Entry<String, Integer> e : list) {
System.out.println(e.getKey() + "::::" + e.getValue());
}
}
public static Map getMapInstance(){
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("apple",40);
map.put("boy",30);
map.put("cat",20);
map.put("dog",10);
return map;
}
public static void printMap(Map map){
Iterator i = map.entrySet().iterator();
while (i.hasNext()) {
Map.Entry<String, Integer> entry = (Map.Entry<String, Integer>) i.next();
System.out.println(entry.getKey() + ":"+ entry.getValue());
}
System.out.println("========================================================");
}
}
Map排序(按key/按value)的更多相关文章
- Map排序——按key排序,按value排序
注:转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/5959279.html 上一篇博文谈到了集合类的自定义排序方式,那么进一步扩展开来,与集合同等重要的Map有 ...
- C++ STL中Map的按Key排序和按Value排序
map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定不存在重名,当然可以对重名加以区 分),我们用map来进 ...
- java Map 之 排序(key,value)
一:起因: (1)现实中须要Map容器进行排序的情况非常多非常多:由于Map<key,value>键值对的存储结构特别是HashMap的结构是非常优秀的,数据存储就难免对其进行排序: (2 ...
- Map排序(按key排序,按value排序)
主要分两种,按键排序.按值排序. 而且,按key排序主要用于TreeMap,而按value排序则对于Map的子类们都适用. 一.按键排序 按Key排序主要用于TreeMap,可以实现按照Key值的大小 ...
- C++ STL中Map的按Key排序跟按Value排序
C++ STL中Map的按Key排序和按Value排序 map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定 ...
- Java Map排序
Map排序的方式有很多种,这里记录下自己总结的两种比较常用的方式:按键排序(sort by key), 按值排序(sort by value). 1.按键排序 jdk内置的java.util包下的Tr ...
- C++ map排序(按照value值排序)_glp_hit_新浪博客
C++ map排序(按照value值排序)_glp_hit_新浪博客 C++ map排序(按照value值排序) (2012-07-12 14:19:51) 转载▼ 标签: ...
- STL容器——对map排序
STL容器(三)——对map排序 对于map的排序问题,主要分为两部分:根据key排序:根据value排序.下面我们就分别说一下~ 1. 根据key进行排序 map默认按照key进行升序排序 ,和输入 ...
- golang中的slice翻转存在以及map中的key判断
//slice翻转 func stringReverse(src []string){ if src == nil { panic(fmt.Errorf("the src can't be ...
随机推荐
- 如何为Linux安装Go语言
导读 Go 语言又称为 golang, 是由 Google 最初开发的一种开源编程语言,其在设计时就遵循了简单.安全和速度的 3 大原则.Go 语言具有多种调试.测试.分析和代码审查工具,如今 Go ...
- js字符串的各种格式的转换 ToString,Format
1.转换钱的格式,仅限int型,float型,double型 double d = 400; d.ToString("C"); //¥400.00 2.10进制数,仅限int型的数 ...
- SQL性能优化十条经验
1.查询的模糊匹配 尽量避免在一个复杂查询里面使用 LIKE '%parm1%'—— 红色标识位置的百分号会导致相关列的索引无法使用,最好不要用. 解决办法: 其实只需要对该脚本略做改进,查询速度便会 ...
- 杂乱无章之Oracle(二)
六.IMPDP用法 1.导入表 impdp hsiufo/hsiufo directory=dump_dir dumpfile=full.dmp tables=scott.emp remap_sche ...
- SQL查询中的in与join效率比较
大多数情况下,程序员比较喜欢使用in来查询符合某些条件的数据,最近在查询某个角色有哪些用户的方法中,使用了in语句: ) FROM baseuser AND BaseUser.Id IN (SELEC ...
- leetcode 题解 Add Two Numbers(两个单链表求和)
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
- HTML 5 History API的”前生今世”
History是有趣的,不是吗?在之前的HTML版本中,我们对浏览历史记录的操作非常有限.我们可以来回使用可以使用的方法,但这就是一切我们能做的了. 但是,利用HTML 5的History API,我 ...
- 2013 ACM/ICPC Asia Regional Online —— Warmup2
HDU 4716 A Computer Graphics Problem 水题.略 HDU 4717 The Moving Points 题目:给出n个点的起始位置以及速度矢量,问任意一个时刻使得最远 ...
- 洛谷P1717 钓鱼
P1717 钓鱼 41通过 116提交 题目提供者该用户不存在 标签贪心 难度提高+/省选- 提交该题 讨论 题解 记录 最新讨论 暂时没有讨论 题目描述 话说发源于小朋友精心设计的游戏被电脑组的童鞋 ...
- GAN
GAN(Generative Adversarial Nets),产生式对抗网络 存在问题: 1.无法表示数据分布 2.速度 3.resolution太小,大了无语义信息 4.无reference 5 ...