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 ...
随机推荐
- ios开发——实用技术篇Swift篇&照片选择
照片选择 // MARK: - 选择照片 /*----- 选择照片 ------*/ @IBAction func addImageButtonClick() { let actionSheet = ...
- iOS开发——UI篇Swift篇&UIDatePicker
UIDatePicker //返回按钮事件 @IBAction func backButtonClick() { self.navigationController?.popViewControlle ...
- Center OS jdk tomcat安装
一.jdk安装 1.下载jdk jdk-6u26-linux-i586-rpm.bin 2 安装jdk #sh jdk-6u26-linux-i586-rpm.bin 3.#set ...
- innobackupex 备份实验
[root@localhost ~]# xtrabackup -v xtrabackup version based Linux (x86_64) (revision id: 45cda89) [ro ...
- BIOS 深入学习 转
http://blog.csdn.net/lightseed/article/category/547391
- 终端I/O之行控制函数
下列4个函数提供了终端设备的行控制能力.其中,filedes引用一个终端设备,否则出错返回,errno设置为ENOTTY. #include <termios.h> int tcdrain ...
- 终端I/O之波特率函数
波特率(baud rate)是一个以往采用的术语,现在它指的是"位/秒"(bits per second).虽然大多数终端设备对输入和输出使用同一波特率,但是只要硬件许可,可以将它 ...
- c#使用MethodInvoker解决跨线程访问控件
功能函数测试集锦(77) C#专区(114) 版权声明:本文为博主原创文章,未经博主允许不得转载. .net 原则上禁止跨线程访问控件,因为这样可能造成错误的发生,有一种方法是禁止编译器对跨线 ...
- 配置Sublime Text 3的Python开发环境
最近的项目是用Python开发自动化测试脚本的,所以使用Python比较多.我用的编辑器是Sublime Text3. Sublime Text 3是一个轻量级的跨平台文字编辑器,一经面世便被认为是一 ...
- C++实现日期转换类DateTime
概述 工作中我们在网络传输时使用time_t来传输时间,在显示时使用字符串来显示,下面是一个日期转换类的实现,方便以后使用: // DateTime.hpp #ifndef _DATETIME_H # ...