package com.ninemax.util.loganalysis.tool;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/**
* map工具类
*
* @author Darker
*
*/
public class MapUtil {
/**
* 获取两个数的百分比
*
* @param n
*
* @param m
*
* @return
*/
public static String seekPercentage(int n, int m) {
// 获取格式化类实例
NumberFormat format = NumberFormat.getPercentInstance();
// 设置小数位
format.setMinimumFractionDigits(2);
return format.format(n / m);
}
/**
* 根据map获取key
*
* @param map
*
* @return
*/
public static String getKey(Map<String, Integer> map){
Iterator<Entry<String, Integer>> it = map.entrySet().iterator();
while (it.hasNext()) {
return it.next().getKey();
}
return null;
}
/**
* 根据map获取value
*
* @param map
*
* @return
*/
public static int getVal(Map<String, Integer> map){
Iterator<Entry<String, Integer>> it= map.entrySet().iterator();
while(it.hasNext()){
return it.next().getValue();
}
return 0;
}
/**
* 根据val获取key
*
* @param value
* @param map
*
* @return
*/
public static String getKeyByVal(int value,Map<String, Integer> map){
int temp=MapUtil.getVal(map);
if(temp==value){
return MapUtil.getKey(map);
}
return null;
}
/**
* 获取value值最大的key
*
* @param map
*
* @return
*/
public static String getKeyByMostValue(Map<String, Integer> map) {
List<Map.Entry<String, Integer>> params = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
// 排序
Collections.sort(params, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1,Map.Entry<String, Integer> o2) {
return (o2.getValue() - o1.getValue());
}
});
return params.get(0).getKey();
}
/**
* 获取value值最小的key
*
* @param map
*
* @return
*/
public static String getKeyByLeastValue(Map<String, Integer> map) {
List<Map.Entry<String, Integer>> params = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
// 排序
Collections.sort(params, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1,Map.Entry<String, Integer> o2) {
return (o1.getValue() - o2.getValue());
}
});
return params.get(0).getKey();
}
/**
* 获取value值最大的map
*
* @param map
*
* @return
*/
public static Map<String, Integer> getMapByMostValue(Map<String, Integer> map) {
// 接收结果集
List<Map.Entry<String, Integer>> params = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
// 返回结果集
Map<String,Integer> resultMap = new HashMap<String,Integer> ();
// 排序
Collections.sort(params, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1,Map.Entry<String, Integer> o2) {
return (o2.getValue() - o1.getValue());
}
});
resultMap.put(params.get(0).getKey(), params.get(0).getValue());
return resultMap;
}
/**
* 获取value值最小的map
*
* @param map
*
* @return
*/
public static Map<String, Integer> getMapByLeastValue(Map<String, Integer> map) {
// 接收结果集
List<Map.Entry<String, Integer>> params = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
// 返回结果集
Map<String,Integer> resultMap = new HashMap<String,Integer> ();
// 排序
Collections.sort(params, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1,Map.Entry<String, Integer> o2) {
return (o1.getValue() - o2.getValue());
}
});
resultMap.put(params.get(0).getKey(), params.get(0).getValue());
return resultMap;
}
/**
* 将map中的key值更改为大写
*
* @param map
* @return
*/
public static Map<String, Object> mapTransformUpperCaseKey(Map<String, Object> map) {
Map<String, Object> resultMap = new HashMap<>();
if (map == null || map.isEmpty()) {
return resultMap;
}
Set<String> keySet = map.keySet();
for (String key : keySet) {
String newKey = key.toUpperCase();
newKey = newKey.replace("_", "");
resultMap.put(newKey, map.get(key));
}
return resultMap;
}
}