如何使用 PropertyUtils 获取数组列表的 属性
How to get property of arraylist using PropertyUtils
我正在使用 Apache 的 PropertUtils 通过传递字符串参数来获取 bean 的值。
在这种特殊情况下,我有对象列表,我想读取列表中对象的特定 属性,相同的代码来解释
List<AuditModelDTO> auditModelDTOs = new ArrayList<>();
AuditModelDTO amd1 = new AuditModelDTO();
amd1.setEntityId("e1");
amd1.setParamResponse(false);
AuditModelDTO amd2 = new AuditModelDTO();
amd2.setEntityId("e2");
amd2.setParamResponse(true);
auditModelDTOs.add(amd1);
auditModelDTOs.add(amd2);
Object requiredObjectProperty = null;
try {
requiredObjectProperty = PropertyUtils.getProperty(auditModelDTOs,"get().entityId");
IndexedProperty(auditModelDTOs,"get(0).entityId",1);
} catch (Exception e) {
log.error("Caller does not have access to the property accessor method. Exception thrown is {}", e);
throw new AuditException(AuditError.ILLEGAL_ACCESS_FOR_PROPERTY_ACCESSOR, e);
}
我想读取列表中所有对象的 entityId
。
有帮助吗?
您可以使用此方法获取 属性 基于对象和 属性 名称的数据
public static String getPropertyValue(Object object, String propertyName) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException
{
String strValue = "";
Class< ? > c = object.getClass();
Field f = c.getDeclaredField(propertyName);
f.setAccessible(true);
Object value = f.get(object);
if (value != null)
{
strValue = value.toString();
}
return strValue;
}
像这样的完整功能:
public static void main(String[] args)
{
List<AuditModelDTO> auditModelDTOs = new ArrayList<>();
AuditModelDTO amd1 = new AuditModelDTO();
amd1.setEntityId("e1");
amd1.setParamResponse(false);
AuditModelDTO amd2 = new AuditModelDTO();
amd2.setEntityId("e2");
amd2.setParamResponse(true);
auditModelDTOs.add(amd1);
auditModelDTOs.add(amd2);
System.out.println("EntityId list : " + getEntityId(auditModelDTOs));
}
// GetEntityIdList
public static List<String> getEntityIdList(List<Object> auditModelDTOs)
throws SecurityException,
IllegalArgumentException,
NoSuchFieldException,
IllegalAccessException
{
List<String> entityIdList = new ArrayList<String>();
String propertyName = "entityId";
for (Object object : auditModelDTOs)
{
if (object != null)
{
entityIdList.add(getPropertyValue(object, propertyName));
}
}
return entityIdList;
}
// getPropertyValue
public static String getPropertyValue(Object object, String propertyName)
throws NoSuchFieldException,
SecurityException,
IllegalArgumentException,
IllegalAccessException
{
String strValue = "";
Class< ? > c = object.getClass();
Field f = c.getDeclaredField(propertyName);
f.setAccessible(true);
Object value = f.get(object);
if (value != null)
{
strValue = value.toString();
}
return strValue;
}
java8 流
List<String> entityIds = auditModelDTOs.streams().map(p-> (String) PropertyUtils.getProperty(p, "entityId")).collect(Collectors.toList());
我正在使用 Apache 的 PropertUtils 通过传递字符串参数来获取 bean 的值。 在这种特殊情况下,我有对象列表,我想读取列表中对象的特定 属性,相同的代码来解释
List<AuditModelDTO> auditModelDTOs = new ArrayList<>();
AuditModelDTO amd1 = new AuditModelDTO();
amd1.setEntityId("e1");
amd1.setParamResponse(false);
AuditModelDTO amd2 = new AuditModelDTO();
amd2.setEntityId("e2");
amd2.setParamResponse(true);
auditModelDTOs.add(amd1);
auditModelDTOs.add(amd2);
Object requiredObjectProperty = null;
try {
requiredObjectProperty = PropertyUtils.getProperty(auditModelDTOs,"get().entityId");
IndexedProperty(auditModelDTOs,"get(0).entityId",1);
} catch (Exception e) {
log.error("Caller does not have access to the property accessor method. Exception thrown is {}", e);
throw new AuditException(AuditError.ILLEGAL_ACCESS_FOR_PROPERTY_ACCESSOR, e);
}
我想读取列表中所有对象的 entityId
。
有帮助吗?
您可以使用此方法获取 属性 基于对象和 属性 名称的数据
public static String getPropertyValue(Object object, String propertyName) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException
{
String strValue = "";
Class< ? > c = object.getClass();
Field f = c.getDeclaredField(propertyName);
f.setAccessible(true);
Object value = f.get(object);
if (value != null)
{
strValue = value.toString();
}
return strValue;
}
像这样的完整功能:
public static void main(String[] args)
{
List<AuditModelDTO> auditModelDTOs = new ArrayList<>();
AuditModelDTO amd1 = new AuditModelDTO();
amd1.setEntityId("e1");
amd1.setParamResponse(false);
AuditModelDTO amd2 = new AuditModelDTO();
amd2.setEntityId("e2");
amd2.setParamResponse(true);
auditModelDTOs.add(amd1);
auditModelDTOs.add(amd2);
System.out.println("EntityId list : " + getEntityId(auditModelDTOs));
}
// GetEntityIdList
public static List<String> getEntityIdList(List<Object> auditModelDTOs)
throws SecurityException,
IllegalArgumentException,
NoSuchFieldException,
IllegalAccessException
{
List<String> entityIdList = new ArrayList<String>();
String propertyName = "entityId";
for (Object object : auditModelDTOs)
{
if (object != null)
{
entityIdList.add(getPropertyValue(object, propertyName));
}
}
return entityIdList;
}
// getPropertyValue
public static String getPropertyValue(Object object, String propertyName)
throws NoSuchFieldException,
SecurityException,
IllegalArgumentException,
IllegalAccessException
{
String strValue = "";
Class< ? > c = object.getClass();
Field f = c.getDeclaredField(propertyName);
f.setAccessible(true);
Object value = f.get(object);
if (value != null)
{
strValue = value.toString();
}
return strValue;
}
java8 流
List<String> entityIds = auditModelDTOs.streams().map(p-> (String) PropertyUtils.getProperty(p, "entityId")).collect(Collectors.toList());