use*_*592 6 java integer java-stream
我有一个方法,它采用整数列表作为参数.我目前有一个很长的列表,并希望将其转换为整数列表,所以我写道:
List<Integer> student =
studentLong.stream()
.map(Integer::valueOf)
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
但我收到一个错误:
method "valueOf" can not be resolved.
Run Code Online (Sandbox Code Playgroud)
实际上是否可以将long列表转换为整数列表?
您应该使用mapToIntwith Long::intValue来提取int值:
List<Integer> student = studentLong.stream()
.mapToInt(Long::intValue)
.boxed()
.collec?t(Collectors.toList(??))
Run Code Online (Sandbox Code Playgroud)
您得到的原因method "valueOf" can not be resolved.是因为没有签名Integer::valueOf接受Long作为参数。
编辑
Per Holger在下面的评论,我们也可以这样做:
List<Integer> student = studentLong.stream()
.map(Long::intValue)
.collec?t(Collectors.toList(??))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5743 次 |
| 最近记录: |