Does Java have an StringStream equivalent?

If you're trying to read a string of numbers from the input and then convert them to integers, you can use the following piece of code which does use Streams.

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the array of integers");

        String str = sc.nextLine();

        String[] strings = str.split(" ");

        List<Integer> ints = Stream.of(strings).
                               map(value -> Integer.valueOf(value)).
                               collect(Collectors.toList());


You can use java.util.Scanner to parse a String using Scanner(String). You can also use java.lang.StringBuilder to construct strings in an efficient manner.

Tags:

Java