Home  >  Article  >  Java  >  How to convert objects to Maps in Java - using BeanMap

How to convert objects to Maps in Java - using BeanMap

PHPz
PHPzforward
2023-05-08 15:49:081890browse

There are many ways to convert javabean and map, such as:

1. Convert bean to json through ObjectMapper, and then convert json to map. However, this method is complicated and inefficient. , after testing, it takes 12 seconds to convert 10,000 beans in a loop! ! ! It is not recommended to use

2. Obtain the attributes and values ​​of the bean class through Java reflection, and then convert them into the key-value pairs corresponding to the map. This method is the second best, but it is a little troublesome

3. Through the method in the net.sf.cglib.beans.BeanMap class, this method is extremely efficient. The difference between it and the second method is that it uses cache. The bean needs to be initialized when it is first created, and then the cache is used. Therefore, it is extremely fast. After testing, it only takes about 300 milliseconds to loop through bean and map conversions 10,000 times.

So, the third method is recommended. The following is the relevant code:

/**
* 将对象装换为map
* @param bean
* @return
*/
public static Map<String, Object> beanToMap(T bean) {
Map<String, Object> map = Maps.newHashMap();
if (bean != null) {
BeanMap beanMap = BeanMap.create(bean);
for (Object key : beanMap.keySet()) {
map.put(key+"", beanMap.get(key));
}
}
return map;
}

/**
* 将map装换为javabean对象
* @param map
* @param bean
* @return
*/
public static T mapToBean(Map<String, Object> map,T bean) {
BeanMap beanMap = BeanMap.create(bean);
beanMap.putAll(map);
return bean;
}

/**
* 将List转换为List<Map<String, Object>>
* @param objList
* @return
* @throws JsonGenerationException
* @throws JsonMappingException
* @throws IOException
*/
public static List<Map<String, Object>> objectsToMaps(List objList) {
List<Map<String, Object>> list = Lists.newArrayList();
if (objList != null && objList.size() > 0) {
Map<String, Object> map = null;
T bean = null;
for (int i = 0,size = objList.size(); i < size; i++) {
bean = objList.get(i);
map = beanToMap(bean);
list.add(map);
}
}
return list;
}

/**
* 将List<Map<String,Object>>转换为List
* @param maps
* @param clazz
* @return
* @throws InstantiationException
* @throws IllegalAccessException
*/
public static List mapsToObjects(List<Map<String, Object>> maps,Class clazz) throws InstantiationException, IllegalAccessException {
List list = Lists.newArrayList();
if (maps != null && maps.size() > 0) {
Map<String, Object> map = null;
T bean = null;
for (int i = 0,size = maps.size(); i < size; i++) {
map = maps.get(i);
bean = clazz.newInstance();
mapToBean(map, bean);
list.add(bean);
}
}
return list;
}

The above is the detailed content of How to convert objects to Maps in Java - using BeanMap. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete