Converting String to Stream of chars

1. Overview

Java 8 introduced the Stream API, with functional-like operations for processing sequences. If you want to read more about it, have a look at this article.

In this quick article, we’ll see how to convert a String to a Stream of single characters.

2. Conversion Using chars()

The String API has a new method – chars() – with which we can obtain an instance of Stream from a String object. This simple API returns an instance of IntStream from the input String.

Simply put, IntStream contains an integer representation of the characters from the String object:

String testString = "String";
IntStream intStream = testString.chars();

It’s possible to work with the integer representation of the characters without converting them to their Character equivalent. This can lead to some minor performance gains, as there will be no need to box each integer into a Character object.

However, if we’re to display the characters for reading, we need to convert the integers to the human-friendly Character form:

Stream<Character> characterStream = testString.chars()
  .mapToObj(c -> (char) c);

3. Conversion Using codePoints()

Alternatively, we can use the codePoints() method to get an instance of IntStream from a String. The advantage of using this API is that Unicode supplementary characters can be handled effectively.

Supplementary characters are represented by Unicode surrogate pairs and will be merged into a single codepoint. This way we can correctly process (and display) any Unicode symbol:

IntStream intStream1 = testString.codePoints();

We need to map the returned IntStream to Stream<Character> to display it to users:

Stream<Character> characterStream2 
  = testString.codePoints().mapToObj(c -> (char) c);

4. Conversion to a Stream of Single Character Strings

So far, we’ve been able to get a Stream of characters; what if we want a Stream of single character Strings instead?

Just as specified earlier in the article, we’ll use either the codePoints() or chars() methods to obtain an instance of IntStream that we can now map to Stream<String>.

The mapping process involves converting the integer values to their respective character equivalents first.

Then we can use String.valueOf() or Character.toString() to convert the characters to a String object:

Stream<String> stringStream = testString.codePoints()
  .mapToObj(c -> String.valueOf((char) c));

5. Conclusion

In this quick tutorial, we learn to obtain a stream of Character from a String object by either calling codePoints() or chars() methods.

This allows us to take full advantage of the Stream API – to conveniently and effectively manipulate characters.

As always, code snippets can be found over on GitHub.

Related posts:

Java Program to Implement HashTable API
Sử dụng CyclicBarrier trong Java
Java Program to Check if a Directed Graph is a Tree or Not Using DFS
Java Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence
Python String endswith()
Simple Single Sign-On with Spring Security OAuth2
Java Program to Implement Slicker Algorithm that avoids Triangulation to Find Area of a Polygon
Using Spring @ResponseStatus to Set HTTP Status Code
How to use the Spring FactoryBean?
Using JWT with Spring Security OAuth
Java Program to Find Transitive Closure of a Graph
Java Program to Implement Selection Sort
Introduction to Eclipse Collections
Lập trình hướng đối tượng (OOPs) trong java
Spring Boot - Thymeleaf
Vector trong Java
Chương trình Java đầu tiên
Java Program to Construct an Expression Tree for an Postfix Expression
Java Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line
Java Program to Find the Longest Subsequence Common to All Sequences in a Set of Sequences
The “final” Keyword in Java
Spring Boot - Tomcat Deployment
Java – Byte Array to Reader
Java Program to Perform String Matching Using String Library
A Guide To UDP In Java
Java Program to Generate Random Hexadecimal Byte
Spring Boot - Internationalization
How to Get a Name of a Method Being Executed?
Java – Convert File to InputStream
Guide to Spring @Autowired
A Guide to Concurrent Queues in Java
Java Program to Implement Dijkstra’s Algorithm using Priority Queue