对象类型和Map相互转化 对象集合和Map集合互相转化

对象类型和Map相互转化 对象集合和Map集合互相转化


import com.google.common.base.Function;
import com.google.common.collect.Lists;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.time.LocalDate;
import java.util.*;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
public class StreamTest {

   

   
   public static List<Student> stus = new ArrayList();
    static {

       Student s1 = new Student("2015134001", "小3", 15, "1501", LocalDate.parse("1940-01-01"),null);
       Student s2 = new Student("2015134003", "小4", 14, "1503",LocalDate.parse("1946-01-01"),"");
       Student s3 = new Student("2015134006", "小5", 15, "1501",LocalDate.parse("1947-01-01"),"hao");
     

       stus.add(s1);
       stus.add(s2);
       stus.add(s3);
      

    }

   public static void main (String[] args) throws IllegalAccessException {



      log.error("长度 {}",stus.size());



      log.info("源对象列表 {}",stus);
      List list = new ArrayList();
      stus.stream().forEach(e->{
         try{
            Map<String, Object> stringObjectMap = objectToMap(e);
            list.add(stringObjectMap);

         }catch(Exception el){

         }


      });
      log.info("map集合对象,{}",list);



   }

   /**
    * 对象转化为map
    * @param o 对象
    * @return 转化成功的map
    * @throws IllegalAccessException 非法访问异常
    */
   private static Map<String,Object> objectToMap(Object o) throws IllegalAccessException {
      if(null == o){
         return null;
      }
      Map<String,Object> map = new HashMap<>();
      Field[] declaredFields = o.getClass().getDeclaredFields();
      for (Field field :declaredFields) {
         // (此处如果不设置 无法获取对象的私有属性)
         field.setAccessible(true);
         //值为null时,不映射为Map
         if(null!=field.get(o)){

            map.put(field.getName(),field.get(o));
         }
      }
      return map;
   }

   /**
    * map 转化为对象
    * @param map 需要转化的参数
    * @param aClass 要转化成的对象
    * @return 转化成功的对象
    * @throws IllegalAccessException 非法访问异常
    * @throws InstantiationException 实例化异常
    */
   private static Object mapToObject(Map<String,Object> map,Class<?> aClass) throws IllegalAccessException, InstantiationException {
      if(null == map || map.size()<=0){
         return null;
      }
      Object o = aClass.newInstance();
      Field[] declaredFields = o.getClass().getDeclaredFields();
      for (Field field :declaredFields) {
         int modifiers = field.getModifiers();
         if(Modifier.isStatic(modifiers) || Modifier.isFinal(modifiers)){
            continue;
         }
         // (此处如果不设置 无法获取对象的私有属性)
         field.setAccessible(true);
         field.set(o,map.get(field.getName()));
      }
      return o;
   }



}
@Data
@Getter
@Setter
@AllArgsConstructor
class Student {

   private String id ;
   private String name ;
   private int  age ;
   private String grade ;
   private LocalDate birthday;
   private String  note;


}
@Data
@NoArgsConstructor
@AllArgsConstructor
class Pepole {

   private String id ;
   private String name ;
   private int  age ;

}

结果

[Student(id=2015134001, name=小3, age=15, grade=1501, birthday=1940-01-01, note=null), Student(id=2015134003, name=小4, age=14, grade=1503, birthday=1946-01-01, note=), Student(id=2015134006, name=小5, age=15, grade=1501, birthday=1947-01-01, note=hao)]
对象,[{birthday=1940-01-01, grade=1501, name=小3, id=2015134001, age=15}, {birthday=1946-01-01, note=, grade=1503, name=小4, id=2015134003, age=14}, {birthday=1947-01-01, note=hao, grade=1501, name=小5, id=2015134006, age=15}]

上一篇:Thymeleaf (详细)


下一篇:『无为则无心』Python序列 — 19、Python列表的其他操作(切片和遍历)