我有一个班级Person
,其字段为String name
。在另一个课程中,我有一个Person
的列表。是否可以使用Java 8流API获取人名列表?
答案 0 :(得分:2)
您可以使用Stream
尝试此操作:
personList.stream().map(p -> p.getName()).collect(Collectors.toList())
答案 1 :(得分:0)
来自http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html#approach9
yourCollection
.stream().map(p -> p.getName())//here you have stream of names
现在你所要做的只是convert this stream to list
.collect(Collectors.toList());
所以试试
List<String> names = yourCollection
.stream()
.map(p -> p.getName())
.collect(Collectors.toList());