【问题标题】:How can I use Lambda to convert a String array to Integer array?如何使用 Lambda 将 String 数组转换为 Integer 数组?
【发布时间】:2015-09-11 17:37:51
【问题描述】:

我正在尝试使用 lambda 表达式将字符串数组转换为整数数组。

我在下面提供了我的代码,以及到目前为止我尝试过的简要说明:

String [] inputData = userInput.split("\\s+");
Integer [] toSort = new Integer [] {};
try {
    toSort = Arrays.asList(inputData).stream().mapToInt(Integer::parseInt).toArray();
}catch(NumberFormatException e) {
    System.out.println("Error. Invalid input!\n" + e.getMessage());
}

我上面的lamba表达式是一个映射到一个int数组的表达式,这不是我想要的,在编译这段代码时,我收到以下错误消息:

BubbleSort.java:13: error: incompatible types: int[] cannot be converted to Integer[]
            toSort = Arrays.asList(inputData).stream().mapToInt(Integer::parseIn
t).toArray();

          ^
1 error

有没有一种简单的方法可以让我使用 lambdas 或其他方式从 String 数组获取 Integer 数组?

【问题讨论】:

  • 只需使用 for 循环 ...

标签: java arrays lambda java-8


【解决方案1】:

正如其他人已经指出的那样,mapToInt 返回一个IntStream,其toArray 方法将返回int[] 而不是Integer[]。除此之外,还有其他一些需要改进的地方:

Integer [] toSort = new Integer [] {};

是初始化数组的一种不必要的复杂方法。使用任一

Integer[] toSort = {};

Integer[] toSort = new Integer[0];

但是你根本不应该初始化它,如果你要覆盖它的话。如果您想为异常情况设置后备值,请在异常处理程序中进行赋值:

String[] inputData = userInput.split("\\s+");
Integer[] toSort;
try {
    toSort = Arrays.stream(inputData).map(Integer::parseInt).toArray(Integer[]::new);
}catch(NumberFormatException e) {
    System.out.println("Error. Invalid input!\n" + e.getMessage());
    toSort=new Integer[0];
}

此外,请注意,您不需要 String[] 数组:

Integer[] toSort;
try {
    toSort = Pattern.compile("\\s+").splitAsStream(userInput)
        .map(Integer::parseInt).toArray(Integer[]::new);
}catch(NumberFormatException e) {
    System.out.println("Error. Invalid input!\n" + e.getMessage());
    toSort=new Integer[0];
}

Pattern 指的是java.util.regex.Pattern,它与String.split 内部使用的类相同。

【讨论】:

  • 但是如果你不介意对int[] 而不是Integer[] 进行排序,你可以接受.mapToInt(Integer::parseInt).toArray() 的结果并将toSort 的类型更改为int[]
【解决方案2】:

mapToInt(Integer::parseInt).toArray() 返回int[] 数组,因为matToInt 产生IntStreamint[] 数组不能被Integer[] 引用使用(装箱仅适用于原始类型,而数组不是)。

你可以使用的是

import java.util.stream.Stream;
//...
toSort = Stream.of(inputData)
               .map(Integer::valueOf) //value of returns Integer, parseInt returns int
               .toArray(Integer[]::new); // to specify type of returned array

【讨论】:

  • 这在变量Stream 上返回了一个错误:BubbleSort.java:13: error: cannot find symbol ... symbol: variable Stream 我需要导入什么东西才能使这个方法起作用吗?
  • @JamesZafar 是的,您需要将导入添加到java.util.stream.Stream;。您可能应该开始使用 IDE,它允许您自动添加导入。
  • IntStream.boxed()
  • @bayou.io: 当然可以,但是当你可以使用map 时,使用mapToInt 后跟boxed() 有点荒谬。
  • @Pshemo:不管你用.map(Integer::valueOf)还是.map(Integer::parseInt);两者都将首先解析为int,然后将其包装为Integer。唯一重要的是您使用的是map 还是mapToInt
【解决方案3】:

如果你想要一个Integer 数组,不要映射到IntStream,映射到Stream<Integer>

toSort = Arrays.asList(inputData).stream().map(Integer::valueOf).toArray(Integer[]::new);

【讨论】:

  • 这返回anotherBubbleSort.java:13: 错误:不兼容的类型:Object[] 无法转换为Integer[] 错误,类似于第一个:BubbleSort.java:13: error: incompatible types: Object[] cannot be converted to I nteger[]
  • @JamesZafar 对不起。我使用了错误的toArray 方法。现已修复。
猜你喜欢
  • 2019-05-10
  • 2023-03-20
  • 2011-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-06
  • 1970-01-01
  • 2019-12-06
相关资源
最近更新 更多