How do we split Strings using Streams?

Overview

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
                  .of(testStr.split(","))
		  .collect(Collectors.toList());
// 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
                       .stream(testStr.split(","))
                       .map(String::trim);
List<String> strList = strStream 
                      .collect(Collectors.toList());
// 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 = Arrays.stream(testStr.split(","))
                  .map(String::trim)
                  .toArray(String[]::new);
// 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
   .chars()
   .mapToObj(ch -> (char) ch)
   .toArray(Character[]::new);
// The array produced is [H, e, l, l, o,  , 1, 2, 3]

Quiz

What will be the output?

	String myStr = "sells";
	String joined = myStr
		.chars()
		.distinct()
		.mapToObj(c -> String.valueOf((char) c))
		.peek(System.out::print)
		.sorted()
		.collect(Collectors.joining());
		System.out.println(" "+ joined);

Choices
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 *