作者:甘文靖 | 来源:互联网 | 2023-01-07 06:58
我有一个方法,它采用整数列表作为参数.我目前有一个很长的列表,并希望将其转换为整数列表,所以我写道:
List student =
studentLong.stream()
.map(Integer::valueOf)
.collect(Collectors.toList());
但我收到一个错误:
method "valueOf" can not be resolved.
实际上是否可以将long列表转换为整数列表?
1> alfasin..:
您应该使用mapToInt
with Long::intValue
来提取int
值:
List student = studentLong.stream()
.mapToInt(Long::intValue)
.boxed()
.collec?t(Collectors.toList(??))
您得到的原因method "valueOf" can not be resolved.
是因为没有签名Integer::valueOf
接受Long
作为参数。
编辑
Per Holger在下面的评论,我们也可以这样做:
List student = studentLong.stream()
.map(Long::intValue)
.collec?t(Collectors.toList(??))
代替`.mapToInt(Long :: intValue).boxed()`,您可以简单地使用`.map(Long :: intValue)`。