Java - map a list of objects to a list with values of their property attributes

后端 未结 8 1797
星月不相逢
星月不相逢 2020-12-01 01:28

I have the ViewValue class defined as follows:

class ViewValue {

private Long id;
private Integer value;
private String description;
private View view;
priv         


        
8条回答
  •  隐瞒了意图╮
    2020-12-01 01:48

    You could do it in a one-liner using Commons BeanUtils and Collections:
    (why write your own code when others have done it for you?)

    import org.apache.commons.beanutils.BeanToPropertyValueTransformer;
    import org.apache.commons.collections.CollectionUtils;
    
    ...
    
    List ids = (List) CollectionUtils.collect(viewValues, 
                                           new BeanToPropertyValueTransformer("id"));
    

提交回复
热议问题