软件编程
位置:首页>> 软件编程>> java编程>> java如何获取map中value的最大值

java如何获取map中value的最大值

作者:wuzi_uzi  发布时间:2023-04-11 05:12:04 

标签:java,map,value,最大值

java获取map中value最大值

public static void main(String[] args) throws InterruptedException {
       Map<Integer, Integer> map = new HashMap<>();
       map.put(1,1);
       map.put(2,2);
       map.put(3,3);
       map.put(4,4);
       System.out.println(getMaxValue(map));
   }
   /**
    * 求Map<K,V>中Value(值)的最小值
    *
    * @param map
    * @return
    */
   public static Object getMinValue(Map<Integer, Integer> map) {
       if (map == null)
           return null;
       Collection<Integer> c = map.values();
       Object[] obj = c.toArray();
       Arrays.sort(obj);
       return obj[0];
   }
   /**
    * 求Map<K,V>中Value(值)的最大值
    *
    * @param map
    * @return
    */
   public static Object getMaxValue(Map<Integer, Integer> map) {
       if (map == null)
           return null;
       int length =map.size();
       Collection<Integer> c = map.values();
       Object[] obj = c.toArray();
       Arrays.sort(obj);
       return obj[length-1];
   }

根据value对Map进行排序得到最大值

import java.util.*;
public class treeMap {
   static String key1 ="";
   static Integer vlue1 ;
   public static void main(String [] arg){
       Map<String,Integer> map = new TreeMap<>();
       map.put("1",2);
       map.put("2",4);
       map.put("3",5);
       map.put("4",12);
       map.put("5",23);
       map.put("6",65);
       map.put("7",1);
       map.put("8",10);
      Map<String , Integer> map1 = new HashMap<>();
       map1 = sortMapByValue(map);
       for (String key : map1.keySet()) {
           key1 =key;
           vlue1=map1.get(key);
           System.out.println("key= "+ key + " and value= " + map1.get(key));
       }
       System.out.println("方位号:"+key1+"\n 距离:"+vlue1);
   }
   public static Map<String, Integer> sortMapByValue(Map<String, Integer> oriMap) {
        class MapValueComparator implements Comparator<Map.Entry<String, Integer>> {
           @Override
           public int compare(Map.Entry<String, Integer> me1, Map.Entry<String, Integer> me2) {
               return me1.getValue().compareTo(me2.getValue());
           }
       }
       if (oriMap == null || oriMap.isEmpty()) {
           return null;
       }
       Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
       List<Map.Entry<String, Integer>> entryList = new ArrayList<Map.Entry<String, Integer>>(
               oriMap.entrySet());
       Collections.sort(entryList, new MapValueComparator());
       Iterator<Map.Entry<String, Integer>> iter = entryList.iterator();
       Map.Entry<String, Integer> tmpEntry = null;
       while (iter.hasNext()) {
           tmpEntry = iter.next();
           sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());
       }
       return sortedMap;
   }
}

获取Map最大value以及对应的key

import  java.util.Arrays;
import  java.util.Collection;
import  java.util.HashMap;
import  java.util.Map;
import  java.util.Set;
public  class  MaxMapDemo {
public  static  void  main(String[] args) {
Map<Integer, Integer> map =  new  HashMap<Integer, Integer>();
map.put( 1 ,  8 );
map.put( 3 ,  12 );
map.put( 5 ,  53 );
map.put( 123 ,  33 );
map.put( 42 ,  11 );
map.put( 44 ,  42 );
map.put( 15 ,  3 );
System.out.println(getMaxKey(map));
System.out.println(getMaxValue(map));
}
/**
* 求Map<K,V>中Key(键)的最大值
* @param map
* @return
*/
public  static  Object getMaxKey(Map<Integer, Integer> map) {
if  (map ==  null )  return  null ;
Set<Integer> set = map.keySet();
Object[] obj = set.toArray();
Arrays.sort(obj);
return  obj[obj.size()- 1 ];
}
/**
* 求Map<K,V>中Value(值)的最大值
* @param map
* @return
*/
public  static  Object getMaxValue(Map<Integer, Integer> map) {
if  (map ==  null )  return  null ;
Collection<Integer> c = map.values();
Object[] obj = c.toArray();
Arrays.sort(obj);
return  obj[obj.size()- 1 ];
}
}

来源:https://blog.csdn.net/qq_37739437/article/details/114397763

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com