文章目录
在Java开发中,经常需要将Java对象转换成Map,或者反过来将Map转换成Java对象。这种转换在很多场景下都非常有用,比如在序列化和反序列化过程中、在数据传输和持久化时、或者在进行对象属性的批量操作时。
本文将介绍几种不同的方法来实现Java对象和Map之间的相互转换,选择哪种方法取决于项目的具体需求和个人偏好。
一、使用Spring Framework的ReflectionUtils
1.1、Bean转为Map
@Test
public void bean2Map(){
Person person=new Person();
person.setName("tiger");
person.setAge(18);
person.setAddress("中国");
person.setCity("深圳");
System.out.println(person);
System.out.println(bean2Map(person));
System.out.println(bean2Map2(person));
}
public static Map<String, Object> bean2Map(Object object) {
Map<String, Object> map = new HashMap<>();
ReflectionUtils.doWithFields(object.getClass(), field -> {
field.setAccessible(true);
Object value = ReflectionUtils.getField(field, object);
if (value != null) {
map.put(field.getName(), value);
}
});
return map;
}
public static Map<String, Object> bean2Map2(Object object) {
Map<String, Object> map = new HashMap<>();
Class<?> clazz = object.getClass();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
try {
Object value = field.get(object);
if (value != null) {
map.put(field.getName(), value);
}
} catch (IllegalAccessException e) {
throw new RuntimeException("Error accessing field: " + field.getName(), e);
}
}
return map;
}
结果输出:
1.2、Map转为Bean
@Test
public void map2bean() throws IllegalAccessException, InstantiationException {
Map<String, Object> map = new HashMap();
map.put("name", "tigerhhzz");
map.put("age", 18);
map.put("address","中国");
map.put("city", "北京");
System.out.println(map);
System.out.println(map2Bean(map, Person.class));
System.out.println(map2Bean2(map, Person.class));
}
public static <T> T map2Bean(Map<String, Object> map, Class<T> clazz) throws IllegalAccessException, InstantiationException {
T instance = clazz.newInstance();
ReflectionUtils.doWithFields(clazz, field -> {
field.setAccessible(true);
if (map.containsKey(field.getName())) {
ReflectionUtils.setField(field, instance, map.get(field.getName()));
}
});
return instance;
}
public static <T> T map2Bean2(Map<String, Object> map, Class<T> clazz) throws IllegalAccessException, InstantiationException {
T instance = clazz.newInstance();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
if (map.containsKey(field.getName())) {
field.set(instance, map.get(field.getName()));
}
}
return instance;
}
结果输出:
二、使用Hutool工具
2.1、引入hutool依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.5.9</version>
</dependency>
2.2、Bean转为Map
@Test
public void bean2Map(){
Person person=new Person();
person.setName("tiger");
person.setAge(18);
person.setAddress("中国");
person.setCity("深圳");
Map<String, Object> map = BeanUtil.beanToMap(person);
System.out.println(person);
System.out.println(bean2Map(person));
System.out.println(bean2Map2(person));
System.out.println(map);
}
结果输出:
2.3、Map转为Bean
@Test
public void map2bean() throws IllegalAccessException, InstantiationException {
Map<String, Object> map = new HashMap();
map.put("name", "tigerhhzz");
map.put("age", 18);
map.put("address","中国");
map.put("city", "北京");
Person person = BeanUtil.toBean(map, Person.class);
System.out.println(map);
System.out.println(map2Bean(map, Person.class));
System.out.println(map2Bean2(map, Person.class));
System.out.println(person);
}
结果输出:
三、使用Jackson工具
3.1、Bean转为Map
@Test
public void bean2Map(){
Person person=new Person();
person.setName("tiger");
person.setAge(18);
person.setAddress("中国");
person.setCity("深圳");
Map<String, Object> map = BeanUtil.beanToMap(person);
System.out.println(person);
System.out.println(bean2Map(person));
System.out.println(bean2Map2(person));
System.out.println(map);
System.out.println(bean2MapByjackson(person));
}
转换方法:
public static Map<String, Object> bean2MapByjackson(Object object) {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.convertValue(object, new TypeReference<Map<String, Object>>() {
});
}
结果输出:
3.2、Map转为Bean
@Test
public void map2bean() throws IllegalAccessException, InstantiationException {
Map<String, Object> map = new HashMap();
map.put("name", "tigerhhzz");
map.put("age", 18);
map.put("address","中国");
map.put("city", "北京");
Person person = BeanUtil.toBean(map, Person.class);
System.out.println(map);
System.out.println(map2BeanByReflectionUtils(map, Person.class));
System.out.println(map2Bean2ByReflectionUtils(map, Person.class));
System.out.println(person);
System.out.println(map2BeanByjackson(map, Person.class));
}
转换方法:
public static <T> T map2BeanByjackson(Map<String, Object> map, Class<T> clazz){
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.convertValue(map, clazz);
}
结果输出:
四、使用Apache Commons Lang的BeanUtils
4.1、引入依赖
<!-- commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
4.2、Bean转为Map
转换方法:
public static Map<String, String> bean2MapBycommonslang3(Object object) {
try {
return BeanUtils.describe(object);
} catch (Exception e) {
throw new RuntimeException("Error converting object to map: " + e.getMessage(), e);
}
}
@Test
public void bean2Map(){
Person person=new Person();
person.setName("tiger");
person.setAge(18);
person.setAddress("中国");
person.setCity("深圳");
Map<String, Object> map = BeanUtil.beanToMap(person);
System.out.println(person);
System.out.println(bean2Map(person));
System.out.println(bean2Map2(person));
System.out.println(map);
System.out.println(bean2MapByjackson(person));
System.out.println(bean2MapBycommonslang3(person));
}
结果输出:
4.3、Map转为Bean
转换方法:
public static <T> T map2BeanBycommonslang3(Map<String, ?> map, Class<T> clazz) {
try {
T instance = clazz.newInstance();
BeanUtils.populate(instance, map);
return instance;
} catch (Exception e) {
throw new RuntimeException("Error converting map to object: " + e.getMessage(), e);
}
}
@Test
public void map2bean() throws IllegalAccessException, InstantiationException {
Map<String, Object> map = new HashMap();
map.put("name", "tigerhhzz");
map.put("age", 18);
map.put("address","中国");
map.put("city", "北京");
Person person = BeanUtil.toBean(map, Person.class);
System.out.println(map);
System.out.println(map2BeanByReflectionUtils(map, Person.class));
System.out.println(map2Bean2ByReflectionUtils(map, Person.class));
System.out.println(person);
System.out.println(map2BeanByjackson(map, Person.class));
System.out.println(map2BeanBycommonslang3(map, Person.class));
}
结果输出:
五、使用fastjson工具
5.1、 引入fastjson依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
5.2、 Bean转为Map
@Test
public void bean2Map(){
Person person=new Person();
person.setName("tiger");
person.setAge(18);
person.setAddress("中国");
person.setCity("深圳");
Map<String, Object> map = BeanUtil.beanToMap(person);
System.out.println(person);
System.out.println(bean2Map(person));
System.out.println(bean2Map2(person));
System.out.println(map);
System.out.println(bean2MapByjackson(person));
System.out.println(bean2MapBycommonslang3(person));
System.out.println(JSONObject.parseObject(JSONObject.toJSONString(person)));
}
结果输出:
5.3、 Map转为Bean
@Test
public void map2bean() throws IllegalAccessException, InstantiationException {
Map<String, Object> map = new HashMap();
map.put("name", "tigerhhzz");
map.put("age", 18);
map.put("address","中国");
map.put("city", "北京");
Person person = BeanUtil.toBean(map, Person.class);
System.out.println(map);
System.out.println(map2BeanByReflectionUtils(map, Person.class));
System.out.println(map2Bean2ByReflectionUtils(map, Person.class));
System.out.println(person);
System.out.println(map2BeanByjackson(map, Person.class));
System.out.println(map2BeanBycommonslang3(map, Person.class));
System.out.println(JSONObject.parseObject(JSONObject.toJSONString(map), Person.class));
}
结果输出:
如果这篇文章对你有所帮助,或者有所启发的话,帮忙 分享、收藏、点赞、在看,你的支持就是我坚持下去的最大动力!