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&lt;Character&gt; characterStream2 
  = testString.codePoints().mapToObj(c -&gt; (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&lt;String&gt; stringStream = testString.codePoints()
  .mapToObj(c -&gt; 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 Generate Randomized Sequence of Given Range of Numbers
Java Program to Find a Good Feedback Edge Set in a Graph
Java Concurrency Interview Questions and Answers
Java Program to Perform Searching Using Self-Organizing Lists
Java Program to Implement Sorting of Less than 100 Numbers in O(n) Complexity
Automatic Property Expansion with Spring Boot
Class Loaders in Java
Hướng dẫn Java Design Pattern – Iterator
Java Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
Convert String to int or Integer in Java
@Lookup Annotation in Spring
Java Program to Implement Graph Coloring Algorithm
Java 8 Predicate Chain
Java Program to Find the Edge Connectivity of a Graph
Functional Interfaces in Java 8
Spring Boot - Creating Docker Image
Spring’s RequestBody and ResponseBody Annotations
Java Program to Implement String Matching Using Vectors
Java Program to Check Whether an Undirected Graph Contains a Eulerian Path
Quick Guide to Spring Bean Scopes
Hashtable trong java
Quản lý bộ nhớ trong Java với Heap Space vs Stack
Java Program to Implement Sieve Of Atkin
Java Program to Implement Find all Forward Edges in a Graph
Count Occurrences of a Char in a String
Java Program to Print only Odd Numbered Levels of a Tree
Java Program to Represent Graph Using Incidence Matrix
Java Program to Find the Mode in a Data Set
Spring Boot - Database Handling
Jackson Unmarshalling JSON with Unknown Properties
Java Program to Compute the Area of a Triangle Using Determinants
Java Program to Implement RoleList API