com.cntool.core.map.MapUtils Maven / Gradle / Ivy
The newest version!
package com.cntool.core.map;
import com.cntool.core.map.emau.MinOrMaxEnum;
import java.math.BigDecimal;
import java.util.*;
/**
* @program: cn-tool
* @description: map集合工具类
* @author: ID-Tang
* @create: 2022-01-18 11:16
* @copyright: Copyright (c) [2022] [ID-tang]
* [cntool] is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
**/
public class MapUtils {
/**
* Map是否为空
*
* @param map 集合
* @return 是否为空
*/
public static boolean isBlank(Map, ?> map) {
return null == map || map.isEmpty();
}
/**
* 获取map集合最大或最小值对应的key
* 该方法仅用来判断value为BigDecimal类型的Map集合
*
* @param map 集合
* @param minOrMaxEnum 最大最小值枚举类 MIN最小值 MAX最大值
* @return 最大或最小值的key
*/
public static String minOrMaxValueKeyBigDecimal(Map, BigDecimal> map, MinOrMaxEnum minOrMaxEnum) {
List> list = new ArrayList<>(map.entrySet());
list.sort(Comparator.comparingDouble(o -> o.getValue().doubleValue()));
if (MinOrMaxEnum.MIN.equals(minOrMaxEnum)) {
return String.valueOf(list.get(0).getKey());
}
return String.valueOf(list.get(list.size() - 1).getKey());
}
/**
* 获取map集合最大或最小值对应的key
* 该方法仅用来判断value为Integer类型的Map集合
*
* @param map 集合
* @param minOrMaxEnum 最大最小值枚举类 MIN最小值 MAX最大值
* @return 最大或最小值的key
*/
public static String minOrMaxValueKeyInteger(Map, Integer> map, MinOrMaxEnum minOrMaxEnum) {
List> list = new ArrayList<>(map.entrySet());
list.sort(Comparator.comparingInt(Map.Entry::getValue));
if (MinOrMaxEnum.MIN.equals(minOrMaxEnum)) {
return String.valueOf(list.get(0).getKey());
}
return String.valueOf(list.get(list.size() - 1).getKey());
}
/**
* 求Map中Value(值)的最大值
*
* @param map 集合
* @return 最大值
*/
public static Object maxValue(Map, ?> map) {
if (isBlank(map)) {
return null;
}
int length = map.size();
Collection> c = map.values();
Object[] obj = c.toArray();
Arrays.sort(obj);
return obj[length - 1];
}
}