James
James

Reputation: 469

How can I use Lambda to convert a String array to Integer array?

I am trying to use lambda expressions to convert a String array to an Integer array.

I have provided my code below, along with a brief description of what I have tried so far:

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());
}

The lamba expression I have above is one which maps to an int array, which is not what I want, upon compiling this code I get the following error message:

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

          ^
1 error

Is there a simple way which allows me to use lambdas, or other means, to get from a String array to an Integer array?

Upvotes: 3

Views: 5329

Answers (3)

Holger
Holger

Reputation: 298389

As already pointed out by others, mapToInt returns an IntStream whose toArray method will return int[] rather than Integer[]. Besides that, there are some other things to improve:

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

is an unnecessarily complicated way to initialized an array. Use either

Integer[] toSort = {};

or

Integer[] toSort = new Integer[0];

but you should not initialize it at all, if you are going to overwrite it anyway. If you want to have a fallback value for the exceptional case, do the assignment inside the exception handler:

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];
}

Further, note that you don’t need the String[] array in your case:

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 refers to java.util.regex.Pattern which is the same class which String.split uses internally.

Upvotes: 6

Pshemo
Pshemo

Reputation: 124265

mapToInt(Integer::parseInt).toArray() returns int[] array since matToInt produces IntStream but int[] array can't be used by Integer[] reference (boxing works only on primitive types, which arrays are not).

What you could use is

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

Upvotes: 6

Eran
Eran

Reputation: 393936

If you want an Integer array, don't map to an IntStream, map to a Stream<Integer> :

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

Upvotes: 2

Related Questions