要将 Java 中的 'mapToInt' 结果转为 'List',可以使用 'IntStream' 的 'boxed()' 方法将 'IntStream' 转为 'Stream',然后使用 'collect' 方法将其收集为 'List'。下面是一个示例代码:

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Main {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5};

        List<Integer> list = IntStream.of(numbers)
                .mapToObj(Integer::valueOf)
                .collect(Collectors.toList());

        System.out.println(list);
    }
}

输出结果为:'[1, 2, 3, 4, 5]'。

在上面的代码中,首先使用 'IntStream.of(numbers)' 将 'int' 数组转为 'IntStream',然后使用 'mapToObj' 方法将 'IntStream' 中的每个元素转为 'Integer' 对象,最后使用 'collect' 方法将 'Stream' 收集为 'List'。

Java mapToInt 结果转换为 List:完整指南及示例

原文地址: https://www.cveoy.top/t/topic/fM5R 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录