【问题标题】:Return stream from array as int[] instead of Integer[] [duplicate]从数组返回流作为 int[] 而不是 Integer[] [重复]
【发布时间】:2020-04-13 12:44:58
【问题描述】:

我有这个数组:

Integer[] originalItems = itemsArray.stream()
            .distinct()
            .sorted()
            .toArray(Integer[]::new);

我想将其返回为 int[] 而不是 Integer[]

我曾尝试致电.toArray(int[]::new),但收到此错误:

不存在类型变量 A 的实例,因此 int[] 符合 A[]

【问题讨论】:

    标签: java arrays integer java-stream


    【解决方案1】:
    .mapToInt(Integer::intValue).toArray();
    

    而不是

    .toArray(Integer[]::new);
    

    因为

    <A> A[] toArray(IntFunction<A[]> generator);
    

    不适用于原始类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-25
      • 1970-01-01
      • 2019-01-12
      • 2013-09-11
      • 1970-01-01
      • 1970-01-01
      • 2015-12-07
      相关资源
      最近更新 更多