java如何获取map中value的最大值
作者: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];
}根据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 ];
}
}总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
