如何对map中的value进行排序
来源:互联网 发布:搞笑淘宝客服对话 编辑:程序博客网 时间:2024/05/18 02:03
package com.demo.map;
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;
/**
* map排序
* @author wang
*
*/
public class SortMap {
public static void main(String[] args) {
Map<String,Integer> map = new HashMap<>();
map.put("nine",9);
map.put("six",6);
map.put("name",6);
map.put("eight",8);
map.put("zero",0);
map.put("one",1);
map.put("four",4);
map.put("two",2);
//根据map中的key值排序
sortMap(map);
}
public static void sortMap(Map<String, Integer> map){
//获取entrySet
Set<Map.Entry<String,Integer>> mapEntries = map.entrySet();
for(Entry<String, Integer> entry : mapEntries){
System.out.println("key:" +entry.getKey()+" value:"+entry.getValue() );
}
//使用链表来对集合进行排序,使用LinkedList,利于插入元素
List<Map.Entry<String, Integer>> result = new LinkedList<>(mapEntries);
//自定义比较器来比较链表中的元素
Collections.sort(result, new Comparator<Entry<String, Integer>>() {
//基于entry的值(Entry.getValue()),来排序链表
@Override
public int compare(Entry<String, Integer> o1,
Entry<String, Integer> o2) {
return o1.getValue().compareTo(o2.getValue()) ;
}
});
//将排好序的存入到LinkedHashMap(可保持顺序)中,需要存储键和值信息对到新的映射中。
Map<String,Integer> linkMap = new LinkedHashMap<>();
for(Entry<String,Integer> newEntry :result){
linkMap.put(newEntry.getKey(), newEntry.getValue());
}
//根据entrySet()方法遍历linkMap
for(Map.Entry<String, Integer> mapEntry : linkMap.entrySet()){
System.out.println("key:"+mapEntry.getKey()+" value:"+mapEntry.getValue());
}
}
}
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;
/**
* map排序
* @author wang
*
*/
public class SortMap {
public static void main(String[] args) {
Map<String,Integer> map = new HashMap<>();
map.put("nine",9);
map.put("six",6);
map.put("name",6);
map.put("eight",8);
map.put("zero",0);
map.put("one",1);
map.put("four",4);
map.put("two",2);
//根据map中的key值排序
sortMap(map);
}
public static void sortMap(Map<String, Integer> map){
//获取entrySet
Set<Map.Entry<String,Integer>> mapEntries = map.entrySet();
for(Entry<String, Integer> entry : mapEntries){
System.out.println("key:" +entry.getKey()+" value:"+entry.getValue() );
}
//使用链表来对集合进行排序,使用LinkedList,利于插入元素
List<Map.Entry<String, Integer>> result = new LinkedList<>(mapEntries);
//自定义比较器来比较链表中的元素
Collections.sort(result, new Comparator<Entry<String, Integer>>() {
//基于entry的值(Entry.getValue()),来排序链表
@Override
public int compare(Entry<String, Integer> o1,
Entry<String, Integer> o2) {
return o1.getValue().compareTo(o2.getValue()) ;
}
});
//将排好序的存入到LinkedHashMap(可保持顺序)中,需要存储键和值信息对到新的映射中。
Map<String,Integer> linkMap = new LinkedHashMap<>();
for(Entry<String,Integer> newEntry :result){
linkMap.put(newEntry.getKey(), newEntry.getValue());
}
//根据entrySet()方法遍历linkMap
for(Map.Entry<String, Integer> mapEntry : linkMap.entrySet()){
System.out.println("key:"+mapEntry.getKey()+" value:"+mapEntry.getValue());
}
}
}
结果为:
key:zero value:0
key:one value:1
key:two value:2
key:four value:4
key:six value:6
key:name value:6
key:eight value:8
key:nine value:9
总结,这里HashMap使用 内部类Entry<K, V>来存储数据,因此,我们需要对存储有键值对的Entry<K,V>对象进行排序来实现我们的HashMap排序的功能。这里采用对象排序的方法,将
Entry<K,V>对象放入到List集合中,再使用自定义比较器对LinkedList集合排序。由于HashMap不存储key-value对,因此采用LinkedHashMap(可保持顺序)中,需要存储键和值信息对到新的映射中。其中要注意的是:Map集合的key是不允许重复的,一单重复等同于覆盖,我在这里就犯错了。
0 0
- 如何对map中的value进行排序
- 对map中的value进行排序
- 对Map中的value进行排序
- Map对value进行排序
- Map 关于按对value进行排序
- 根据value字段对map进行排序
- 对Map按value进行排序
- java对Map按Value进行排序
- 对map根据value进行排序
- 根据value值对map进行排序
- java 对map中的 value 排序
- 对hashMap中的value进行排序
- 对hashMap中的value进行排序
- 关于Map类型中对value进行排序的问题
- 对Map进行按值(value)排序
- C++中实现对map按照value值进行排序
- STL 对map的key和value进行大小排序
- 对Map的key和value进行排序
- mongodb集群搭建-replica set模式
- 2015年网易公司校招笔试编程+3轮面试
- Linux下jboss6.1.0日志输出设置
- Unity3d中BlinnPhong光照模型注解
- AIX下取前一天时间
- 如何对map中的value进行排序
- Android应用程序发送广播(sendBroadcast)的过程分析
- java中super和this关键字解析
- ZigBee使用Z-Stack协议栈时如何关闭ACK验证
- 2009
- android 线程初识
- android蛋疼的OOM
- 数据库设计原则
- 论实践和认识的关系--来自生活体验和感悟