Map中根据value排序---输出value最大时所对应的Key
2013-05-23 12:02
645 查看
一 相关知识
Map內涵资料是Key -> Value的架构集合体,而Key是属于Set的架构集合体,也就是说Key的值是唯一的,而Value的值可以重复。
一般常用的子类是HashMap或TreeMap,
如果考虑效能的话,建议使用HashMap,
如果希望Key值有顺序性,就使用TreeMap吧!
二 根据Value的大小进行排序。(EntrySet+Comparator)
Map<String , Integer> mapInteger = new HashMap<String , Integer>();
mapInteger.put("A", 98);
mapInteger.put("B", 50);
mapInteger.put("C", 76);
mapInteger.put("D", 23);
mapInteger.put("E", 85);
map中对整数value值排序操作如下:
public static void mapSortInteger(Map<String , Integer> map){
List<Map.Entry<String, Integer>> listData = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
System.out.println("排序前listData:");
System.out.println(listData);
//排序----// //当value值为整型时,
compare()方法
Collections.sort(listData, new Comparator<Map.Entry<String, Integer>>(){
public int compare(Map.Entry<String,Integer > o1, Map.Entry<String, Integer> o2){
return (o2.getValue() - o1.getValue());
}
}
);
// //当value值为双精度浮点类型时,
compare()方法
Collections.sort(listData,
new Comparator<Map.Entry<String, Double>>(){
public int compare(Map.Entry<String,Double
> o1, Map.Entry<String, Double> o2){
return
(int)((o2.getValue() - o1.getValue()) * 1000);
}
System.out.println("排序后listData:");
System.out.println(listData);
}
输出结果为:
========整型数据操作========
排序前listData:
[D=23, E=85, A=98, B=50, C=76]
排序后listData:
[A=98, E=85, C=76, B=50, D=23]
三 取得Value最大时对应的Key值的俩种方法
1 通过EntrySet<K,V>,将value放到一个List中,利用集合方法类Collections的sort方法,进行排序.(默认自然升序),然后找出等于最大value的所有key.
public static void main(String[] args){
Map map=new HashMap();
map.put("d", 761);
map.put("g", 7);
map.put("a", 761);
map.put("c", 34);
int value=0;
String maxKey = null;
List list=new ArrayList();
Iterator ite=map.entrySet().iterator();
while(ite.hasNext()){
// EntrySet 将同一个元素的key与value 关联在一起,当当前元素的value等于按从小到大排序的最后(最大)的元素的value值
//,输出当前/元素的键key与值value
Map.Entry entry =(Map.Entry)ite.next();
value = Integer.parseInt(entry.getValue().toString());
list.add(entry.getValue());
Collections.sort(list);
if(value == Integer.parseInt(list.get(list.size()-1).toString())){
maxKey = entry.getKey().toString();
System.out.println(maxKey+"/"+value);
}
}
}
}
// 答案:
// d/761
// a/761
2 先取的value的最大值,然后找出等于最大value的所有key。
public static void main(String[] args) {
Map map=new HashMap();
map.put("d", 761);
map.put("g", 7);
map.put("a", 761);
map.put("c", 34);
Set set=map.keySet();
Iterator it=set.iterator();
int max=0;
while(it.hasNext()){
String key=(String)it.next();
int init=(Integer)map.get(key);
if(max<(Integer)map.get(key)){
max=(Integer)map.get(key);
}
}
List<String> list=test(map,max);
for(String s:list){
System.out.print(s+"/");
System.out.println(max);
}
}
private static List test(Map map, Object o) {
List list=new ArrayList();
Set set=map.keySet();
Iterator it=set.iterator();
while(it.hasNext()){
String key=(String)it.next();
if(o.equals(map.get(key))){
list.add(key);
}
}
return list;
}
}
// 答案:
// d/761
// a/761
Map內涵资料是Key -> Value的架构集合体,而Key是属于Set的架构集合体,也就是说Key的值是唯一的,而Value的值可以重复。
一般常用的子类是HashMap或TreeMap,
如果考虑效能的话,建议使用HashMap,
如果希望Key值有顺序性,就使用TreeMap吧!
二 根据Value的大小进行排序。(EntrySet+Comparator)
Map<String , Integer> mapInteger = new HashMap<String , Integer>();
mapInteger.put("A", 98);
mapInteger.put("B", 50);
mapInteger.put("C", 76);
mapInteger.put("D", 23);
mapInteger.put("E", 85);
map中对整数value值排序操作如下:
public static void mapSortInteger(Map<String , Integer> map){
List<Map.Entry<String, Integer>> listData = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
System.out.println("排序前listData:");
System.out.println(listData);
//排序----// //当value值为整型时,
compare()方法
Collections.sort(listData, new Comparator<Map.Entry<String, Integer>>(){
public int compare(Map.Entry<String,Integer > o1, Map.Entry<String, Integer> o2){
return (o2.getValue() - o1.getValue());
}
}
);
// //当value值为双精度浮点类型时,
compare()方法
Collections.sort(listData,
new Comparator<Map.Entry<String, Double>>(){
public int compare(Map.Entry<String,Double
> o1, Map.Entry<String, Double> o2){
return
(int)((o2.getValue() - o1.getValue()) * 1000);
}
System.out.println("排序后listData:");
System.out.println(listData);
}
输出结果为:
========整型数据操作========
排序前listData:
[D=23, E=85, A=98, B=50, C=76]
排序后listData:
[A=98, E=85, C=76, B=50, D=23]
三 取得Value最大时对应的Key值的俩种方法
1 通过EntrySet<K,V>,将value放到一个List中,利用集合方法类Collections的sort方法,进行排序.(默认自然升序),然后找出等于最大value的所有key.
public static void main(String[] args){
Map map=new HashMap();
map.put("d", 761);
map.put("g", 7);
map.put("a", 761);
map.put("c", 34);
int value=0;
String maxKey = null;
List list=new ArrayList();
Iterator ite=map.entrySet().iterator();
while(ite.hasNext()){
// EntrySet 将同一个元素的key与value 关联在一起,当当前元素的value等于按从小到大排序的最后(最大)的元素的value值
//,输出当前/元素的键key与值value
Map.Entry entry =(Map.Entry)ite.next();
value = Integer.parseInt(entry.getValue().toString());
list.add(entry.getValue());
Collections.sort(list);
if(value == Integer.parseInt(list.get(list.size()-1).toString())){
maxKey = entry.getKey().toString();
System.out.println(maxKey+"/"+value);
}
}
}
}
// 答案:
// d/761
// a/761
2 先取的value的最大值,然后找出等于最大value的所有key。
public static void main(String[] args) {
Map map=new HashMap();
map.put("d", 761);
map.put("g", 7);
map.put("a", 761);
map.put("c", 34);
Set set=map.keySet();
Iterator it=set.iterator();
int max=0;
while(it.hasNext()){
String key=(String)it.next();
int init=(Integer)map.get(key);
if(max<(Integer)map.get(key)){
max=(Integer)map.get(key);
}
}
List<String> list=test(map,max);
for(String s:list){
System.out.print(s+"/");
System.out.println(max);
}
}
private static List test(Map map, Object o) {
List list=new ArrayList();
Set set=map.keySet();
Iterator it=set.iterator();
while(it.hasNext()){
String key=(String)it.next();
if(o.equals(map.get(key))){
list.add(key);
}
}
return list;
}
}
// 答案:
// d/761
// a/761
相关文章推荐
- Map排序,获取map的第一值,根据value取key等操作(数据预处理)
- Java中Map根据键值(key)或者值(value)进行排序实现
- map排序,根据key给map排序,根据value给map排序
- Java中Map根据键值(key)或者值(value)进行排序实现
- Java中Map根据键值(key)或者值(value)进行排序实现
- 找出一个数组中重复次数最多的字符暨找出Map中的最大Value及其对应的Key
- Java中Map根据键(Key)或者值(Value)进行排序
- java map遍历、排序,根据value获取key
- Map根据Valuse 输出对应的key(一)
- Map根据Valuse 输出对应的key(二)
- 获取map中根据key进行排序的value数据
- 算法实战:根据Key或Value对Map进行排序及其应用
- Java中Map根据键值(key)或者值(value)进行排序实现
- LinkedHashMap实现分组排序(保证有序性),并实现map的一个key值对应多个value
- 算法实战:根据Key或Value对Map进行排序及其应用
- Java中Map根据键值(key)或者值(value)进行排序实现
- map获取最大值value和其对应的key
- Java 中 Map 根据键值 (key) 或者值 (value) 进行排序实现
- 根据value取得map中对应的key
- Java基础 —— 根据 Key 或是 Value 对 Map 进行排序