Java8 Transform list of object to list of one attribute of object

后端 未结 1 1227
迷失自我
迷失自我 2020-12-06 11:45

I want to use Java 8 tricks to do the following in one line.

Given this object definition:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
pub         


        
1条回答
  •  有刺的猬
    2020-12-06 12:45

    This should do the trick:

    objects.stream().map(MyObj::getId).collect(Collectors.toList());

    that said, the method reference :: operator allows you to reference any method in your classpath and use it as a lambda for the operation that you need.

    As mentioned in the comments, a stream preserves order.

    0 讨论(0)
提交回复
热议问题