我怎么能将long列表转换为整数列表.我写 :
longList.stream().map(Long::valueOf).collect(Collectors.toList())
//longList is a list of long.
Run Code Online (Sandbox Code Playgroud)
我有一个错误:
Incompatible types. Required iterable<integer> but collect was inferred to R.
Run Code Online (Sandbox Code Playgroud)
愿有人能告诉我如何解决这个问题吗?
Ous*_* D. 11
你需要Long::intValue而不是Long::valueOf因为这个函数返回的Long类型不是int.
Iterable<Integer> result = longList.stream()
.map(Long::intValue)
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
或者如果您希望接收器类型为List<Integer>:
List<Integer> result = longList.stream()
.map(Long::intValue)
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)