正如评论中提到的,流会保留顺序.I want to use Java 8 tricks to do the following in one line.Given this object definition:@Getter@Setter@NoArgsConstructor@AllArgsConstructorpublic class MyObj { private String id; private Double value;}and a List<MyObj> objects, I want to get a List<String> objectIds which is a list of all ids of the objects in the first list - in the same order.I can do this using a loop in Java but I believe there should be a one-liner lambda in Java8 that can do this. I was not able to find a solution online. Perhaps I wasn't using the right search terms.Could someone suggest a lambda or another one-liner for this transform? 解决方案 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. 这篇关于Java8 将对象列表转换为对象的一个​​属性列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-29 03:51