如何将Java 8 Stream转换为数组?

本文翻译自:How to convert a Java 8 Stream to an Array?

将Java 8 Stream转换为数组的最简单/最快捷的方法是什么?


#1楼

参考:https://stackoom.com/question/1Yptz/如何将Java-Stream转换为数组


#2楼

The easiest method is to use the toArray(IntFunction<A[]> generator) method with an array constructor reference. 最简单的方法是将toArray(IntFunction<A[]> generator)方法与数组构造函数引用一起使用。 This is suggested in the API documentation for the method . API文档中建议该方法

String[] stringArray = stringStream.toArray(String[]::new);

What it does is find a method that takes in an integer (the size) as argument, and returns a String[] , which is exactly what (one of the overloads of) new String[] does. 它的作用是找到一个以整数(大小)作为参数的方法,并返回String[] ,而这正是new String[] (的重载之一)所做的事情。

You could also write your own IntFunction : 您也可以编写自己的IntFunction

Stream<String> stringStream = ...;
String[] stringArray = stringStream.toArray(size -> new String[size]);

The purpose of the IntFunction<A[]> generator is to convert an integer, the size of the array, to a new array. IntFunction<A[]> generator是将整数(数组的大小)转换为新数组。

Example code: 示例代码:

Stream<String> stringStream = Stream.of("a", "b", "c");
String[] stringArray = stringStream.toArray(size -> new String[size]);
Arrays.stream(stringArray).forEach(System.out::println);

Prints: 打印:

a
b
c

#3楼

If you want to get an array of ints, with values form 1 to 10, from a Stream, there is IntStream at your disposal. 如果要从Stream中获取一个整数数组,其值的形式为1到10,则可以使用IntStream。

Here we create a Stream with a Stream.of method and convert an Stream to an IntStream using a mapToInt. 在这里,我们使用Stream.of方法创建一个Stream,并使用mapToInt将Stream转换为IntStream。 Then we can call IntStream's toArray method. 然后我们可以调用IntStream的toArray方法。

扫描二维码关注公众号,回复: 10780217 查看本文章
Stream<Integer> stream = Stream.of(1,2,3,4,5,6,7,8,9,10);
//or use this to create our stream 
//Stream<Integer> stream = IntStream.rangeClosed(1, 10).boxed();
int[] array =  stream.mapToInt(x -> x).toArray();

Here is the same thing, without the Stream, using only the IntStream 这是同一件事,没有Stream,仅使用IntStream

int[]array2 =  IntStream.rangeClosed(1, 10).toArray();

#4楼

You can create a custom collector that convert a stream to array. 您可以创建将流转换为数组的自定义收集器。

public static <T> Collector<T, ?, T[]> toArray( IntFunction<T[]> converter )
{
    return Collectors.collectingAndThen( 
                  Collectors.toList(), 
                  list ->list.toArray( converter.apply( list.size() ) ) );
}

and a quick use 和快速使用

List<String> input = Arrays.asList( ..... );

String[] result = input.stream().
         .collect( CustomCollectors.**toArray**( String[]::new ) );

#5楼

     Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6);

     Integer[] integers = stream.toArray(it->new Integer[it]);

#6楼

You can convert a java 8 stream to an array using this simple code block: 您可以使用以下简单代码块将Java 8流转换为数组:

 String[] myNewArray3 = myNewStream.toArray(String[]::new);

But let's explain things more, first, let's Create a list of string filled with three values: 但是,让我们进一步解释一下,首先,我们创建一个包含三个值的字符串列表:

String[] stringList = {"Bachiri","Taoufiq","Abderrahman"};

Create a stream from the given Array : 从给定的Array创建一个流:

Stream<String> stringStream = Arrays.stream(stringList);

we can now perform some operations on this stream Ex: 我们现在可以在此流Ex上执行一些操作:

Stream<String> myNewStream = stringStream.map(s -> s.toUpperCase());

and finally convert it to a java 8 Array using these methods: 最后使用以下方法将其转换为Java 8 Array:

1-Classic method (Functional interface) 1-经典方法(功能接口)

IntFunction<String[]> intFunction = new IntFunction<String[]>() {
    @Override
    public String[] apply(int value) {
        return new String[value];
    }
};


String[] myNewArray = myNewStream.toArray(intFunction);

2 -Lambda expression 2-Lambda表达式

 String[] myNewArray2 = myNewStream.toArray(value -> new String[value]);

3- Method reference 3-方法参考

String[] myNewArray3 = myNewStream.toArray(String[]::new);

Method reference Explanation: 方法参考说明:

It's another way of writing a lambda expression that it's strictly equivalent to the other. 这是编写lambda表达式的另一种方法,它与另一方法完全等效。

发布了0 篇原创文章 · 获赞 51 · 访问量 34万+

猜你喜欢

转载自blog.csdn.net/CHCH998/article/details/105471097
今日推荐