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


I have the ViewValue class defined as follows:

class ViewValue {

private Long id;
private Integer value;
private String description;
private View view;
private Double defaultFeeRate;

// getters and setters for all properties

Somewhere in my code i need to convert a list of ViewValue instances to a list containing values of id fields from corresponding ViewValue.

I do it using foreach loop:

List<Long> toIdsList(List<ViewValue> viewValues) {

   List<Long> ids = new ArrayList<Long>();

   for (ViewValue viewValue : viewValues) {

   return ids;


Is there a better approach to this problem?

Best Solution

We can do it in a single line of code using java 8

List<Long> ids =;

For more info : Java 8 - Streams