【问题标题】:Does streams use Boxing and unboxing when print array of int values打印int值数组时流是否使用装箱和拆箱
【发布时间】:2020-09-20 10:02:58
【问题描述】:

如果我运行这行代码:

Arrays.stream(new int[] {2, 4, 6, 8, 10}).forEach(x->System.out.print(x+ " " ));

值是否有装箱或拆箱?

【问题讨论】:

  • 您可以使用 boxed() 方法将其装箱为整数。试试这个例子:.List list = Arrays.stream(new int[] {2, 4, 6, 8, 10}) .sorted() .boxed() .collect(Collectors.toList());

标签: java java-stream


【解决方案1】:

不,没有自动装箱/自动拆箱发生。您调用 IntStream stream(int[] array) 处理 int[]int 而不涉及任何 IntegerforEach 得到一个 IntConsumer,它只处理 int。并且字符串连接似乎也不涉及装箱(String Concatenation and Autoboxing in Java),因为使用了StringBuilder.append(int)

【讨论】:

    猜你喜欢
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 2011-07-03
    • 1970-01-01
    • 2012-07-15
    • 2011-12-25
    • 2011-02-28
    相关资源
    最近更新 更多