[Java] List转换为实体类的属性 →→→→→进入此内容的聊天室

来自 , 2020-11-25, 写在 Java, 查看 103 次.
URL http://www.code666.cn/view/22fb0cee
  1. package model;
  2.  
  3. import java.lang.reflect.Field;
  4. import java.lang.reflect.Method;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7.  
  8. public class ListToModel {
  9.         public static void main(String[] args) {
  10.                 List<Object> userList = new ArrayList<Object>();
  11.                 userList.add("ooP");
  12.                 userList.add("男");
  13.                 userList.add(18);
  14.                 User user = new User();
  15.                 try {
  16.                         listToModel(userList, user);
  17.                 } catch (Exception e) {
  18.                         e.printStackTrace();
  19.                 }
  20.                 System.out.println(user.getName() + "; " + user.getGender() + "; " + user.getAge());
  21.  
  22.         }
  23.  
  24.         public static <T> void listToModel(List<Object> list, T t) throws Exception {
  25.                 Field[] fields = t.getClass().getDeclaredFields();
  26.                 if (list.size() != fields.length) {
  27.                         return;
  28.                 }
  29.                 for (int k = 0, len = fields.length; k < len; k++) {
  30.                         // 根据属性名称,找寻合适的set方法
  31.                         String fieldName = fields[k].getName();
  32.                         String setMethodName = "set" + fieldName.substring(0, 1).toUpperCase()
  33.                                         + fieldName.substring(1);
  34.                         Method method = null;
  35.                         Class<?> clazz = t.getClass();
  36.                         try {
  37.                                 method = clazz.getMethod(setMethodName, new Class[] { list.get(k).getClass() });
  38.                         } catch (SecurityException e1) {
  39.                                 e1.printStackTrace();
  40.                                 return;
  41.                         } catch (NoSuchMethodException e1) {
  42.                                 String newMethodName = "set" + fieldName.substring(0, 1).toLowerCase()
  43.                                                 + fieldName.substring(1);
  44.                                 try {
  45.                                         method = clazz.getMethod(newMethodName, new Class[] { list.get(k).getClass() });
  46.                                 } catch (SecurityException e) {
  47.                                         e.printStackTrace();
  48.                                         return;
  49.                                 } catch (NoSuchMethodException e) {
  50.                                         e.printStackTrace();
  51.                                         return;
  52.                                 }
  53.                         }
  54.                         if (method == null) {
  55.                                 return;
  56.                         }
  57.                         method.invoke(t, new Object[] { list.get(k) });
  58.                 }
  59.         }
  60.  
  61. }
  62.  

回复 "List转换为实体类的属性"

这儿你可以回复上面这条便签

captcha