gpt4 book ai didi

java - 使用 Java 8 将 long 列表转换为可迭代的整数

转载 作者:搜寻专家 更新时间:2023-10-31 08:18:46 25 4
gpt4 key购买 nike

如何将长列表转换为整数列表。我写道:

longList.stream().map(Long::valueOf).collect(Collectors.toList())
//longList is a list of long.

我有一个错误:

Incompatible types. Required iterable<integer> but collect was inferred to R.

谁能告诉我如何解决这个问题?

最佳答案

你需要 Long::intValue 而不是 Long::valueOf 因为此函数返回 Long输入不是 int .

Iterable<Integer> result = longList.stream()
.map(Long::intValue)
.collect(Collectors.toList());

或者如果您希望接收器类型为 List<Integer> :

List<Integer> result = longList.stream()
.map(Long::intValue)
.collect(Collectors.toList());

关于java - 使用 Java 8 将 long 列表转换为可迭代的整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48104515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com