Array.map
Java中是否有相当于Javascript的东西?
我一直在玩Java 8:
List<Long> roleList = siteServiceList.stream()
.map(s -> s.getRoleIdList()).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
但这不起作用我不知道为什么警告说Incompatible Type
.
我怎么能在Java8中这样做?
如果roleIdList
是a List<Long>
而你想得到一个List<Long>
你必须使用flatMap
:
List<Long> roleList = siteServiceList.stream()
.flatMap(s -> s.getRoleIdList().stream())
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
如果你坚持使用map
返回类型应该是List<List<Long>>
:
List<List<Long>> roleList = siteServiceList.stream()
.map(MyObject::getRoleIdList)
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2253 次 |
最近记录: |