Pratik
Pratik

Reputation: 992

How to get property of arraylist using PropertyUtils

I am using Apache's PropertUtils to get values of bean by passing string parameter. In this particular case I have List of Object and I want to read specific property of an object inside the list, same code to explain

 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);
    } 

I want to read entityId of all the object inside the list. Any help?

Upvotes: 0

Views: 3007

Answers (3)

kuhajeyan
kuhajeyan

Reputation: 11077

java8 streams

List<String> entityIds = auditModelDTOs.streams().map(p-> (String) PropertyUtils.getProperty(p, "entityId")).collect(Collectors.toList());

Upvotes: 2

BIZ
BIZ

Reputation: 113

The full feature like that :

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;
}

Upvotes: 0

BIZ
BIZ

Reputation: 113

You can use this method to get data of property base on object and property name

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;
    }

Upvotes: 0

Related Questions