如何将 Java 8 IntStream 转换为列表? [英] How do I convert a Java 8 IntStream to a List?

查看:52
本文介绍了如何将 Java 8 IntStream 转换为列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看 IntStream 的文档,我看到了一个 toArray 方法,但没有办法直接转到 List;

I'm looking at the docs for the IntStream, and I see an toArray method, but no way to go directly to a List<Integer>

当然有一种方法可以将 Stream 转换为 List 吗?

Surely there is a way to convert a Stream to a List?

推荐答案

IntStream::boxed

IntStream::boxed 变成了 IntStreamStream,然后您可以collectList:

theIntStream.boxed().collect(Collectors.toList())

boxed 方法将 IntStreamint 原始值转换为 Integer 对象.boxing"这个词将int命名为code> ⬌ Integer 转换过程.请参阅 Oracle 教程.

The boxed method converts the int primitive values of an IntStream into a stream of Integer objects. The word "boxing" names the intInteger conversion process. See Oracle Tutorial.

Java 16 带来了更短的 toList 方法.产生一个 unmodifiable清单.在此处讨论.

Java 16 brought the shorter toList method. Produces an unmodifiable list. Discussed here.

theIntStream.boxed().toList() 

这篇关于如何将 Java 8 IntStream 转换为列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆