Arrays.stream(array) vs Arrays.asList(array).stream()[英] Arrays.stream(array) vs Arrays.asList(array).stream()

本文是小编为大家收集整理的关于Arrays.stream(array) vs Arrays.asList(array).stream()的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

本文来自:IT宝库(https://www.itbaoku.cn)

这个问题已经回答了两种表达式都是相等的,但是在这种情况下,它们会产生不同的结果.对于给定的int[] scores,为什么要工作:

Arrays.stream(scores)
        .forEach(System.out::println);

...但这不是:

Arrays.asList(scores).stream()
        .forEach(System.out::println);
据我所知,.stream()可以在任何集合上调用,肯定是列表.第二个代码段只是返回包含整体数组而不是元素的流.

推荐答案

您看到的行为不是特定于Stream s的. Arrays.asList(scores)返回List<int[]>当您传递给int[]时,由于无法用原始类型代替通用类型的参数.因此,当您调用asList(T... a)时,编译器使用int[]代替T.

如果将scores更改为Arrays.asList(scores),您将获得期望的输出(即Arrays.asList(scores)将返回List<Integer>).

其他推荐答案

第二代码段不起作用的原因是,Java中没有List<int>.这就是为什么Arrays.asList(scores)不产生您的期望.

从int[]切换到Integer[]将解决此问题,从某种意义上说,这两个代码将产生相同的结果.但是,您的代码效率较低,因为所有int s都将被装箱.

实际上,效率是原始阵列过载stream的原因.您的调用Arrays.stream(scores)被路由到stream(int[] array),生成IntStream对象.现在,您可以应用.forEach(System.out::println),该>调用println(int),再次避免进行拳击.

其他推荐答案

阵列. 它不会在编译时间上抱怨,因为原始数组是一个对象.

它可以将一个对象作为列表,但是对象(原始的数组是一个对象)不能转换为列表.

原始数组可以使用 IntStream,DoubleStream和LongStream

喜欢这个

double[] doubleArray = {1.1,1.2,1.3};

DoubleStream.of(doubleArray).forEach(System.out::println);


int[] intArray = {1,2,3,4,5,6};

IntStream.of(intArray).forEach(System.out::println);


long[] longArray = {1L,2L,3L};

LongStream.of(longArray).forEach(System.out::println);

本文地址:https://www.itbaoku.cn/post/975302.html

问题描述

In this question it has already been answered that both expressions are equal, but in this case they produce different results. For a given int[] scores, why does this work:

Arrays.stream(scores)
        .forEach(System.out::println);

...but this does not:

Arrays.asList(scores).stream()
        .forEach(System.out::println);

As far as I know .stream() can be called on any Collection, which Lists definitely are. The second code snippet just returns a stream containing the array as a whole instead of the elements.

推荐答案

The behavior you see is not specific to Streams. Arrays.asList(scores) returns a List<int[]> when you pass to it an int[], since a generic type parameter can't be replaced by a primitive type. Therefore, when you call asList(T... a), the compiler uses int[] in place of T.

If you change scores to Integer[], you'll get the output you expect (i.e. Arrays.asList(scores) will return a List<Integer>).

其他推荐答案

The reason the second code snippet does not work is that there is no such thing as List<int> in Java. That is why Arrays.asList(scores) does not produce what you expect.

Switching from int[] to Integer[] would fix this problem, in the sense that the two pieces of code would produce identical results. However, your code would be less efficient, because all ints would be boxed.

In fact, efficiency is the reason behind having overloads of stream for primitive arrays. Your call Arrays.stream(scores) is routed to stream(int[] array), producing IntStream object. Now you can apply .forEach(System.out::println), which calls println(int), again avoiding boxing.

其他推荐答案

Arrays.asList expects arrays of Object not arrays of primitives. It does not complain on compile time because arrays of primitive is an object.

It can take one object as list but what is inside that object(arrays of primitive is an object) cannot be convert to list.

Arrays of primitive can be convert to stream using IntStream,DoubleStream and LongStream

like this

double[] doubleArray = {1.1,1.2,1.3};

DoubleStream.of(doubleArray).forEach(System.out::println);


int[] intArray = {1,2,3,4,5,6};

IntStream.of(intArray).forEach(System.out::println);


long[] longArray = {1L,2L,3L};

LongStream.of(longArray).forEach(System.out::println);