概述
public class MapUtil {
public static final char UNDERLINE = '_';
/**
* 下划线方式转驼峰
* @param
*/
public static Map underlineToCamel(Map param) {
if (param == null) {
return null;
}
Map retMap = new HashMap();
Set list = param.keySet();
for (String string : list) {
String str = string.toLowerCase();
int len = str.length();
StringBuilder sb = new StringBuilder(len);
for (int i = 0; i < len; i++) {
char c = str.charAt(i);
if (c == UNDERLINE) {
if (++i < len) {
sb.append(Character.toUpperCase(str.charAt(i)));
}
} else {
sb.append(c);
}
}
String key = sb.toString();
retMap.put(key, param.get(string));
}
return retMap;
}
/**
* map转javabean
* @param
*/
public static T convertMap(Class type, Map map)
throws IntrospectionException, IllegalAccessException,
InstantiationException, InvocationTargetException {
BeanInfo beanInfo = Introspector.getBeanInfo(type); // 获取类属性
T obj = (T) type.newInstance(); // 创建 JavaBean 对象
// 给 JavaBean 对象的属性赋值
PropertyDescriptor[] propertyDescriptors = beanInfo
.getPropertyDescriptors();
for (int i = 0; i < propertyDescriptors.length; i++) {
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName();
if (map.containsKey(propertyName)) {
// 下面一句可以 try 起来,这样当一个属性赋值失败的时候就不会影响其他属性赋值。
Object value = map.get(propertyName);
Object[] args = new Object[1];
args[0] = value;
descriptor.getWriteMethod().invoke(obj, args);
}
}
return obj;
}
public static Map sumMapForString(Map a,
Map b) {
/**
* @Return java.util.Map
* @Description 将两个Map拼接成一个
* @param a
* @param b
*/
Map res = new HashMap();
for (Map.Entry aMap : a.entrySet()) {
res.put(aMap.getKey(), aMap.getValue());
}
for (Map.Entry bMap : b.entrySet()) {
if (res.containsKey(bMap.getKey())) {
String key = bMap.getKey();
int aValue = Integer.valueOf(res.get(key));
int bValue = Integer.valueOf(bMap.getValue());
int temp = aValue + bValue;
res.put(key, temp + "");
} else {
res.put(bMap.getKey(), bMap.getValue());
}
}
return b;
}
public static Map sumMapForInteger(Map a,
Map b) {
/**
* @Return java.util.Map
* @Description 将两个Map拼接成一个
* @param a
* @param b
*/
Map res = new HashMap();
for (Map.Entry aMap : a.entrySet()) {
res.put(aMap.getKey(), aMap.getValue());
}
for (Map.Entry bMap : b.entrySet()) {
if (res.containsKey(bMap.getKey())) {
String key = bMap.getKey();
int aValue = res.get(key);
int bValue = bMap.getValue();
int temp = aValue + bValue;
res.put(key, temp);
} else {
res.put(bMap.getKey(), bMap.getValue());
}
}
return res;
}
/**
* Map转换成Map
*
* @param map
* @return
*/
public static Map objectToString(Map map) {
Map res = new HashMap();
for (Map.Entry iterable : map.entrySet()) {
if(!(iterable.getValue() ==null)){
res.put(iterable.getKey(), iterable.getValue().toString());
}else {
res.put(iterable.getKey(), "");
}
}
return res;
}
/**
* 将Map中的value取出存入List
*
* @param map
* @return
*/
public static List mapToList(Map map) {
List list = new ArrayList<>();
for (Map.Entry string : map.entrySet()) {
list.add(string.getValue());
}
return list;
}
/**
* MODULE : sortMapByValue
* ABSTRACT : 将map的值从大到小排序
* @author : BJ
* @param :HashMap
* @return :TreeMap
* @note : 将map的值从大到小排序
*/
public static TreeMap sortMapByValue(HashMap map){
Comparator comparator = new ValueComparator(map);
//TreeMap is a map sorted by its keys.
//The comparator is used to sort the TreeMap by keys.
TreeMap result = new TreeMap(comparator);
result.putAll(map);
return result;
}
}
class ValueComparator implements Comparator{
HashMap map = new HashMap();
public ValueComparator(HashMap map){
this.map.putAll(map);
}
@Override
public int compare(String s1, String s2) {
if(map.get(s1) >= map.get(s2)){
return -1;
}else{
return 1;
}
}
}
最后
以上就是成就发带为你收集整理的java 自定义map_Java的map自定义工具类MapUtils的全部内容,希望文章能够帮你解决java 自定义map_Java的map自定义工具类MapUtils所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复