java如何獲取map中value的最大值
更新時(shí)間:2023年05月26日 10:27:03 作者:wuzi_uzi
這篇文章主要介紹了java如何獲取map中value的最大值問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
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進(jìn)行排序得到最大值
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("方位號(hào):"+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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
如何通過一個(gè)注解實(shí)現(xiàn)MyBatis字段加解密
用戶隱私很重要,因此很多公司開始做數(shù)據(jù)加減密改造,下面這篇文章主要給大家介紹了關(guān)于如何通過一個(gè)注解實(shí)現(xiàn)MyBatis字段加解密的相關(guān)資料,需要的朋友可以參考下2022-02-02
SpringCloud Ribbon負(fù)載均衡實(shí)例解析
這篇文章主要介紹了SpringCloud Ribbon負(fù)載均衡實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
Oracle + Mybatis實(shí)現(xiàn)批量插入、更新和刪除示例代碼
利用MyBatis動(dòng)態(tài)SQL的特性,我們可以做一些批量的操作,下面這篇文章主要給大家介紹了關(guān)于Oracle + Mybatis實(shí)現(xiàn)批量插入、更新和刪除的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2018-01-01
Java中雙冒號(hào)運(yùn)算符(::)的用法詳解
在Java 8引入的Lambda表達(dá)式和函數(shù)式接口之后,雙冒號(hào)運(yùn)算符(::)成為了一項(xiàng)重要的功能,下面我們就來學(xué)習(xí)一下Java中的雙冒號(hào)運(yùn)算符及其常見應(yīng)用場景吧2023-12-12
Java中將List拆分為多個(gè)小list集合的實(shí)現(xiàn)代碼
這篇文章主要介紹了Java中如何將List拆分為多個(gè)小list集合,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
SpringBoot + validation 接口參數(shù)校驗(yàn)的思路詳解
這篇文章主要介紹了SpringBoot + validation 接口參數(shù)校驗(yàn),本文通過項(xiàng)目實(shí)踐+場景分析給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
Java中的可變參數(shù)常見用法實(shí)例總結(jié)
這篇文章主要介紹了Java中的可變參數(shù)常見用法,結(jié)合實(shí)例形式總結(jié)分析了java可變參數(shù)的常見功能、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-10-10

