Java对象转Map<String,String>

Java对象转Map<String,String>

import org.springframework.beans.BeanUtils;
import org.springframework.util.ReflectionUtils;

    private static Map<String, String> convertObjectToMap(Object obj){
        return Arrays.stream(BeanUtils.getPropertyDescriptors(obj.getClass()))
                .filter(pd -> !"class".equals(pd.getName()))
                .collect(HashMap::new,
                        (map, pd) -> {
                            if(ReflectionUtils.invokeMethod(pd.getReadMethod(), obj) != null) {
                                map.put(pd.getName(), String.valueOf(ReflectionUtils.invokeMethod(pd.getReadMethod(), obj)));
                            }
                        },
                        HashMap::putAll);
    }

 

posted on 2022-06-09 17:05  oktokeep  阅读(1396)  评论(0编辑  收藏  举报