You can exclude one step if parsing the string directly in Integer:
String line = "1 2 3 4 5"; List<Integer> elements = Arrays.stream(line.split(" ")).map(Integer::valueOf) .collect(Collectors.toList());
Or you can stick with primitive types that give better performance by creating an int array instead of List<Integer> :
int[] elements = Arrays.stream(line.split(" ")).mapToInt(Integer::parseInt).toArray ();
You can also replace
Arrays.stream(line.split(" "))
with
Pattern.compile(" ").splitAsStream(line)
I am not sure which is more efficient.
source share