要将Java中的mapToInt结果转为List,可以使用IntStream的boxed()方法将IntStream转为Stream<Integer>,然后使用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<Integer>收集为List。

java mapToInt 转为list

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

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