2016-08-09 10 views
0

私はApacheのPropertUtilsを使用して、文字列パラメータを渡してBeanの値を取得しています。私は、リスト内のすべてのオブジェクトのentityIdを読みたいPropertyUtilsを使用してarraylistのプロパティを取得する方法

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

を説明するために、私は、オブジェクトのリストを持っていると私は、リスト内のオブジェクトの特定のプロパティを読みたい、同じコードこの特定のケースでは 。 ヘルプがありますか?

あなたがオブジェクトとプロパティ名をプロパティベースのデータを取得するには、このメソッドを使用することができます

答えて

1

java8はあなたが

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

ありがとう、私は似たようなことをやり始めましたが、あなたのJava 8のソリューションはそれをもっと簡単にしました。 – Pratik

0

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

ことができますストリーム質問に表示されているListの例でこれをどのように使うことができるのか説明してください。 – Pratik

0

ようなフル機能:

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; 
} 
関連する問題