Map工具类

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;
    }
}

 

转载于:https://my.oschina.net/Tsher2015/blog/708123

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值