我有一个方法,它接受一个整数列表作为参数。 我目前有一个长列表,想将其转换为整数列表,所以我写道:
List<Integer> student =
studentLong.stream()
.map(Integer::valueOf)
.collect(Collectors.toList());
但是我收到一个错误:
method "valueOf" can not be resolved.
是否真的可以将长整数列表转换为整数列表?
最佳答案
您应该使用带有 Long::intValue
的 mapToInt
来提取 int
值:
List<Integer> student = studentLong.stream()
.mapToInt(Long::intValue)
.boxed()
.collect(Collectors.toList())
您得到 method "valueOf"cannot be resolved.
的原因是因为没有 Integer::valueOf
的签名它接受 Long
作为参数。
编辑
根据 Holger 在下面的评论,我们还可以:
List<Integer> student = studentLong.stream()
.map(Long::intValue)
.collect(Collectors.toList())
关于java - 将 long 列表转换为 int java 8 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47543573/