18

I'm currently learning how to use Java and my friend told me that this block of code can be simplified when using Java 8. He pointed out that the parseIntArray could be simplified. How would you do this in Java 8?

public class Solution {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String[] tokens = input.nextLine().split(" ");
        int[] ints = parseIntArray(tokens);
    }

    static int[] parseIntArray(String[] arr) {
        int[] ints = new int[arr.length];
        for (int i = 0; i < ints.length; i++) {
            ints[i] = Integer.parseInt(arr[i]);
        }
        return ints;
    }
}
Holger
  • 285,553
  • 42
  • 434
  • 765
  • 2
    Does this answer your question? [Converting a String Array to an Int array](https://stackoverflow.com/questions/21677530/converting-a-string-array-to-an-int-array) – Mr. Jain Aug 08 '20 at 17:17

3 Answers3

47

For example:

static int[] parseIntArray(String[] arr) {
    return Stream.of(arr).mapToInt(Integer::parseInt).toArray();
}

So take a Stream of the String[]. Use mapToInt to call Integer.parseInt for each element and convert to an int. Then simply call toArray on the resultant IntStream to return the array.

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
  • Wow! Is this java? Certainly this is tidier than my solution. Thank you for the fast response! – Irvin Denzel Torcuato Oct 11 '14 at 10:17
  • 1
    @IrvinDenzelTorcuato Java 8, yes. – Boris the Spider Oct 11 '14 at 10:18
  • 3
    @MohammadFaisal it's Java 8 syntax, read about it in [the tutorial](http://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html). – Boris the Spider Oct 11 '14 at 10:32
  • How do you get it as Integer[] array instead of int[] array? – Ridhuvarshan Nov 29 '18 at 06:38
  • What have you tried @Ridhuvarshan? Why didn't it work? – Boris the Spider Nov 29 '18 at 06:43
  • I have tried the same thing. It works and I am getting an int[] array. I need an integer array. I can get it by passing the int[] array using this code https://stackoverflow.com/a/27043087/5779242, but is it possible in one step? – Ridhuvarshan Nov 29 '18 at 06:48
  • How would you get an `Integer[]` from any other `Stream`? Can you use the code you linked to do it without copying? Why does my code generate an `int[]` but the linked code generate an `Integer[]`? @Ridhuvarshan – Boris the Spider Nov 29 '18 at 06:51
  • The linked answer is used to convert an `int[]` array to `Integer[]`. Your code is to convert `String[]` to `int[]`. I want `String[]` to `Integer[]` in one step. – Ridhuvarshan Nov 29 '18 at 06:55
  • @Ridhuvarshan indeed. Can you combine the two to do that? What does the linked answer do with the `IntStream`? Does my answer feature an `IntStresm`? How could you combine these two stream processing fragments? – Boris the Spider Nov 29 '18 at 06:57
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/184450/discussion-between-ridhuvarshan-and-boris-the-spider). – Ridhuvarshan Nov 29 '18 at 06:58
19

You may skip creating the token String[] array:

Pattern.compile(" ")
       .splitAsStream(input.nextLine()).mapToInt(Integer::parseInt).toArray();

The result of Pattern.compile(" ") may be remembered and reused, of course.

Holger
  • 285,553
  • 42
  • 434
  • 765
2

You could, also, obtain the array directly from a split:

String input; //Obtained somewhere
...
int[] result = Arrays.stream(input.split(" "))
        .mapToInt(Integer::valueOf)
        .toArray();

Here, Arrays has some nice methods to obtain the stream from an array, so you can split it directly in the call. After that, call mapToInt with Integer::valueOf to obtain the IntStream and toArray for your desired int array.

Shirkam
  • 744
  • 6
  • 20