How do we split Strings using Streams?


Many common use cases such as splitting, trimming etc may involve generating streams from strings in Java. In this article, we shall explore some String API that comes in very handy to handle such cases.

Splitting a String into a List (based on a delimiter)

It is possible to split a delimiter (such as comma) separated string into a List by using the split() method of String API along with Streams API as shown below.

String testStr = " Hello,how,are, you";
List<String> testStrList = Stream
// The list produced is [ Hello, how, are,  you]

Removing white spaces while splitting

If the String contains some leading, trailing, or extra white spaces around the delimiter, the trim() method can be used along with the split() method as shown in the below example.

Stream<String> strStream = Arrays
List<String> strList = strStream 
// The list produced is [Hello, how, are, you]

Splitting a String into an array (based on a delimiter)

Instead of List, if the result is to be collected into an array, the same example can be rewritten as below.

String[] strArray =","))
// The array produced is [Hello, how, are, you]

Converting a String into a List of Characters

The chars() method in the String API converts the String into a stream of Integer objects where each Integer represents the ASCII value of each and every character in the string. The Integer representations of characters need to be boxed to Character objects using the mapToObj() method as shown in the below example.

String test = "Hello 123";
Character[] charArr = test
   .mapToObj(ch -> (char) ch)
// The array produced is [H, e, l, l, o,  , 1, 2, 3]


What will be the output?

	String myStr = "sells";
	String joined = myStr
		.mapToObj(c -> String.valueOf((char) c))
		System.out.println(" "+ joined);

A. sel els
B. sells ellss
C. sel sel
D. None of the above

Choice A is correct. The above code prints “sel els” when run. The chars() method returns an IntStream representing the characters. The distinct() method removes the duplicates, then the resulting character stream is boxed to actual characters. A peek() call at this point returns the characters ‘s’, ‘e’ and ‘l’ only. After sorting the order becomes ‘e’, ‘l’ and then ‘s’. Hence A is correct and the other choices are incorrect.

Leave a Reply

Your email address will not be published. Required fields are marked *