javaBean与map集合的互相转换

本文详细介绍三种将Java对象转换为Map及从Map转换回Java对象的方法:通过反射操作属性字段,利用内省通过属性的读写方法进行设置,以及使用BeanUtils工具类简化操作。适用于Java开发者理解和掌握对象与Map之间的转换技巧。

方式一通过反射的方法,通过属性字段操作

public static Map<String, Object> bean2Map1(Object obj) throws IllegalAccessException {
    if (obj == null)
        return Collections.EMPTY_MAP;

    Map<String,Object> map = new HashMap<>();

    //获取所有的属性
    Field[] fields = obj.getClass().getDeclaredFields();
    for (Field field : fields){
        //设置属性是可以进入的,私有属性也可以进入
        field.setAccessible(true);
        map.put(field.getName(),field.get(obj));
    }
    return map;
}

public static <T> T map2Bean1(Map<String,Object> map, Class<T> clazz) throws IllegalAccessException, InstantiationException, NoSuchFieldException {
    if (map == null)
        return null;

    T obj = clazz.newInstance();

    Set<String> keys = map.keySet();
    for (String key : keys){
        Field field = clazz.getDeclaredField(key);
        field.setAccessible(true);
        field.set(obj,map.get(key));
    }
    return obj;
}

方式2,通过内省,通过属性的读写方法进行设置

public static Map<String,Object> bean2Map2(Object obj) throws IntrospectionException, InvocationTargetException, IllegalAccessException {
    if (obj == null)
        return Collections.EMPTY_MAP;


    Map<String,Object> map = new HashMap<>();

    BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
    //通过beanInfo获取属性描述
    PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
    for (PropertyDescriptor propertyDescriptor : propertyDescriptors){
        String key = propertyDescriptor.getName();
        if (!"class".equals(key)) {
            Method getter = propertyDescriptor.getReadMethod();
            Object value = getter.invoke(obj);
            map.put(key, value);
        }
    }
    return map;
}

public static <T> T map2Bean2(Map<String, Object> map, Class<T> clazz) throws IllegalAccessException, InstantiationException, IntrospectionException, InvocationTargetException {
    if (map == null)
        return null;

    T obj = clazz.newInstance();

    Set<String> set = map.keySet();
    BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
    PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
    for (PropertyDescriptor propertyDescriptor : propertyDescriptors){
        String key = propertyDescriptor.getName();
        if (!"class".equals(key)) {
            Method writer = propertyDescriptor.getWriteMethod();
            writer.invoke(obj, map.get(key));
        }
    }
    return obj;
}

//方式3,使用BeanUtils工具类

public static Map<String,Object> bean2Map3(Object obj) throws Exception {
     if (obj == null)
         return Collections.EMPTY_MAP;

     return BeanUtils.describe(obj);
 }

 public static <T> T map2Bean3(Map<String, Object> map, Class<T> beanClass) throws Exception {
     if (map == null)
         return null;

     T obj = beanClass.newInstance();

     BeanUtils.populate(obj, map);
     return obj;
 }	
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值