java如何獲取map中value的最大值
更新時(shí)間:2023年05月26日 10:27:03 作者:wuzi_uzi
這篇文章主要介紹了java如何獲取map中value的最大值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(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對(duì)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以及對(duì)應(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)文章
Java利用Jackson序列化實(shí)現(xiàn)數(shù)據(jù)脫敏
這篇文章主要介紹了利用Jackson序列化實(shí)現(xiàn)數(shù)據(jù)脫敏,首先在需要進(jìn)行脫敏的VO字段上面標(biāo)注相關(guān)脫敏注解,具體實(shí)例代碼文中給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-10-10
java使用mysql預(yù)編譯語(yǔ)句查詢優(yōu)勢(shì)及示例詳解
這篇文章主要為大家介紹了java使用mysql預(yù)編譯語(yǔ)句的優(yōu)勢(shì)特點(diǎn)及示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
Springboot+QueryDsl實(shí)現(xiàn)融合數(shù)據(jù)查詢
這篇文章主要將介紹的是 Springboot 使用 QueryDsl 實(shí)現(xiàn)融合數(shù)據(jù)查詢,文中有詳細(xì)的代碼講解,對(duì) SpringBoot?Querydsl?查詢操作感興趣的朋友一起看看吧2023-08-08
Java遞歸遍歷樹形結(jié)構(gòu)的實(shí)現(xiàn)代碼
這篇文章主要介紹了Java遞歸遍歷樹形結(jié)構(gòu)的相關(guān)資料,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2016-03-03
spring boot(三)之Spring Boot中Redis的使用
這篇文章主要介紹了spring boot(三)之Spring Boot中Redis的使用,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-05-05
Mybatis動(dòng)態(tài)查詢字段及表名的實(shí)現(xiàn)
本文主要介紹了Mybatis動(dòng)態(tài)查詢字段及表名的實(shí)現(xiàn),通過靈活運(yùn)用Mybatis提供的動(dòng)態(tài)SQL功能,我們可以構(gòu)建更加靈活、高效的查詢語(yǔ)句,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2024-01-01

