3

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?

1
  • just use a for loop ... Commented Sep 11, 2015 at 17:48

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

But if you don’t mind sort an int[] instead of Integer[], you could just live with the result of .mapToInt(Integer::parseInt).toArray() and change the type of toSort to int[].
6

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

6 Comments

This has returned an error on the variable Stream: BubbleSort.java:13: error: cannot find symbol ... symbol: variable Stream Is there something I need to import for this method to work?
@JamesZafar Yes, you need to add import to java.util.stream.Stream;. You should probably start using IDE which will allow you to add imports automatically.
or IntStream.boxed()
@bayou.io: sure, but it would be a bit nonsensical to use mapToInt just to be followed by boxed() when you can use map in the first place.
@Pshemo: it doesn’t matter whether you use .map(Integer::valueOf) or .map(Integer::parseInt); both will first parse to an int and then wrap it to an Integer. The only thing that matters is whether you use map or mapToInt.
|
2

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

2 Comments

This returns anotherBubbleSort.java:13: error: incompatible types: Object[] cannot be converted to I nteger[] error for me, similar to the first one: BubbleSort.java:13: error: incompatible types: Object[] cannot be converted to I nteger[]
@JamesZafar Sorry. I used the wrong toArray method. Fixed now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.