Java 8:将IntStream转换为Integer [] [英] Java 8: IntStream to Integer[]

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

问题描述

我正在编写一个简单的程序,该程序最终将绘制用Java编写的各种排序算法的运行时间.排序算法的常规接口通过以下方法:public void sort(Comparable[] xs)

I am writing simple program which will eventually plot the run times of various sorting algorithms written in Java. The general interface of a sorting algorithm is via a method: public void sort(Comparable[] xs)

我正在尝试使用Java 8的流机制按照以下几行生成随机测试用例:

I am attempting to use Java 8's stream mechanism to generate random test cases along the following lines:

public static IntStream testCase(int min, int max, int n) {
    Random generator = new Random();
    return generator.ints(min, max).limit(n);
}

我的问题是,如何将类型为IntStream的对象转换为Integer[]?

My question is, how can I convert an object of type IntStream to an Integer[]?

推荐答案

您应

You should box the IntStream to a Stream<Integer>, then call toArray to make a array of it:

Integer[] arr = testCase(1,2,3).boxed().toArray(Integer[]::new);

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

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