参考: 添加链接描述

获取List以及List中类的属性值的参考代码:

public static List getListFieldValueByObject (Object object , String targetFieldName) throws Exception {
    List<Object> returnList = new ArrayList<>();
    // 获取该对象的Class
    Class objClass = object.getClass();
    // 获取所有的属性数组
    Field[] fields = objClass.getDeclaredFields();
    for (Field field:fields) {
        // 判断属性是否是List类型
        if(field.getGenericType() instanceof List){
            // 对应的属性值,假设泛型List<YouObject>
             * 举例:
             * @JsonProperty("xi_you_ji")
             * private List<XiYouJi> xiYouJi;
            List<YouObject> fieldValue = (List) field.get(object);
            // 获取List泛型中的对象的Class
            Class youObjectClass = YouObject.getClass(); // 此处同样可以反射获取List对应的对象无需限定死
            Field[] youObjectFields = youObjectClass.getDeclaredFields();
            // 遍历List对象属性
            for (YouObject youObject :fieldValue) {
                // 从对象中获取目标属性值
                for (Field youObjectField : youObjectFields) {
                    // 属性名称
                    String currentFieldName = "";
                    // 获取属性上面的注解 import com.fasterxml.jackson.annotation.JsonProperty;
                     *  举例:
                     *   @JsonProperty("di_ren_jie")
                     *   private String diRenJie;
                    boolean has_JsonProperty =  field.isAnnotationPresent(JsonProperty.class);
                    if(has_JsonProperty){
                        currentFieldName = field.getAnnotation(JsonProperty.class).value();
                    }else {
                        currentFieldName = field.getName();
                    if(currentFieldName.equals(targetFieldName)){
                        returnList.add(field.get(object));
    return returnList;

获取所有属性值的代码:

		Student st = new Student(1,"zhuangsan",78,98);
		Field[] fields = st.getClass().getDeclaredFields();
		for(int i = 0 ; i < fields.length ; i++) {
		    //设置是否允许访问,不是修改原来的访问权限修饰词。
			fields[i].setAccessible(true);
			System.out.println(fields[i].getName()+":"+fields[i].get(st));
	Field[] fields = valueObject.getClass().getDeclaredFields();
	for (Field field : fields) {
    	if ("LIST_NAME".equalsIgnoreCase(field.getName())) {
        	field.setAccessible(true);
            List<YourObject> fieldValue = (List) field.get(valueObject);
            // fieldValue即为获取到的List     
			Assert.assertEquals(fieldValue.size(), 2);
			Assert.assertTrue(fieldValue.get(0).getSourceName().equalsIgnoreCase("DB"));
                    参考:添加链接描述获取List以及List中类的属性值的参考代码:public static List getListFieldValueByObject (Object object , String targetFieldName) throws Exception {    List&lt;Object&gt; returnList = new ArrayList&lt;&gt;();    // 获取该对象的Class    Class objClass = object.getClas
				
反射的定义、机制等等概念网上有很多,这里就不做过多的赘述了,本文主要介绍通过反射机制获取对象一个list属性的,并将到另一个对象。 1.实体类 package com.study.reflex; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor public class Women { private Integer id; private String na
除了int等基本类型外,Java的其他类型全部都是class(包括interface) class(包括interface)的本质是数据类型(Type)。无继承关系的数据类型无法赋Number n = new Double(123.456); // OK String s = new Double(123.456); // compile error! JVM持有的每 首先是需要引入的依赖项,如下: implementation 'org.assertj:assertj-core:3.21.0' implementation 'org.springframework:spring-aspects:5.3.13' implementation 'com.github.ulisesbocchio:jasypt-spring-boot-starter:3.0.4 import com.wm.javayu.model.Student; import com.wm.javayu.model.Teacher; import org.springframework.util.CollectionUtils; import org.springframework.util.ObjectUtil...
正则获取 括号内的内容一、 介绍二、小结 一、 介绍 我们正常反射 获取方法Method 里面的 参数类型, 但是 如果参数时 List , 只能拿到List, 获取不到对应的Object 是什么类型,思路如下: class -> .getMethod() -> Method.getParameters() -> parameter.getDeclaringExecutable().toGenericString(); -> 获取 public abstract void co
;打开文件夹 path = file__select(path = './', title = '选择文件夹') file_list = file_search(path + '*.txt') ;循环处理每个文件 nfile = n_elements(file_list) ave_list = fltarr(nfile) for i=0, nfile-1 do begin filepath = file_list[i] ave_ref = getAverageReflectance(filepath) ave_list[i] = ave_ref print, '文件', filepath, '的反射率平均为:', ave_ref 该代码首先定义了一个函数`getAverageReflectance(filename)`,该函数可以实现读取反射率数据并计算反射率平均,然后可以读取指定文件夹下的所有文件,循环调用该函数获取每个文件的反射率平均,并输出文件路径及反射率平均。最终,反射率平均列表将存储在数组`ave_list`