java如何獲取map中value的最大值
更新時間:2023年05月26日 10:27:03 作者:wuzi_uzi
這篇文章主要介紹了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]; }
根據(jù)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以及對應(yīng)的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 ]; } }
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringCloud Ribbon負(fù)載均衡實例解析
這篇文章主要介紹了SpringCloud Ribbon負(fù)載均衡實例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11Oracle + Mybatis實現(xiàn)批量插入、更新和刪除示例代碼
利用MyBatis動態(tài)SQL的特性,我們可以做一些批量的操作,下面這篇文章主要給大家介紹了關(guān)于Oracle + Mybatis實現(xiàn)批量插入、更新和刪除的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2018-01-01Java中將List拆分為多個小list集合的實現(xiàn)代碼
這篇文章主要介紹了Java中如何將List拆分為多個小list集合,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03SpringBoot + validation 接口參數(shù)校驗的思路詳解
這篇文章主要介紹了SpringBoot + validation 接口參數(shù)校驗,本文通過項目實踐+場景分析給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10