相关疑难解决方法(0)

Java - 将对象列表映射到包含其属性属性值的列表

我有如下定义的ViewValue类:

class ViewValue {

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

// getters and setters for all properties
}
Run Code Online (Sandbox Code Playgroud)

在我的代码中的某处,我需要将ViewValue实例列表转换为包含来自相应ViewValue的id字段值的列表.

我使用foreach循环来做到这一点:

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

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

   for (ViewValue viewValue : viewValues) {
      ids.add(viewValue.getId());
   }

   return ids;
Run Code Online (Sandbox Code Playgroud)

}

有没有更好的方法来解决这个问题?

java properties list

34
推荐指数
4
解决办法
7万
查看次数

标签 统计

java ×1

list ×1

properties ×1