com.github.easilyuse.common.util.MapUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of easily-http Show documentation
Show all versions of easily-http Show documentation
Easy implementation of http client remote calls through annotations
The newest version!
package com.github.easilyuse.common.util;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
*
* Title:MapUtil
*
*
* Description: map相关工具类
*
*
* @author linyb
*/
public class MapUtil {
private static Logger logger = LoggerFactory.getLogger(MapUtil.class);
/**
* key首字母转大写
*
* @param map
* @return 返回key首字母大写,value不为空的map集合
*/
public static Map getCapwordsKeyMap(Map map) {
Map capWordsKeyMap = new HashMap<>();
if (!map.isEmpty()) {
map.forEach((key, value) -> {
String capwordsKey = StringUtil.capitalize(key);
if (StringUtil.isNotBlank(value)) {
capWordsKeyMap.put(capwordsKey, value);
}
});
}
return capWordsKeyMap;
}
/**
* 对象映射成key为首字母大写的map
*
* @param t
* @return 返回key首字母大写,value不为空的map集合
*/
public static Map getCapwordsKeyMap(T t) {
Map params = new HashMap<>();
try {
params = BeanUtils.describe(t);
params.remove("class");
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return getCapwordsKeyMap(params);
}
/**
* 对象映射成map
*
* @param t
* @return 返回value不为空的map集合
*/
public static Map convertBean2Map(T t) {
Map newParams = new HashMap<>();
try {
Map params = BeanUtils.describe(t);
params.remove("class");
params.forEach((key, value) -> {
if (StringUtil.isNotBlank(value)) {
newParams.put(key, value);
}
});
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return newParams;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy