Java StringBuilder codePoints() Method

The StringBuilder.codePoints() method in Java is used to create an IntStream of Unicode code point values from a StringBuilder object. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. codePoints Method Syntax
  3. Examples
    • Creating an IntStream from a StringBuilder
    • Processing Code Points with Streams
    • Filtering and Collecting Code Points
  4. Conclusion

Introduction

The StringBuilder.codePoints() method is a member of the StringBuilder class in Java. It allows you to obtain an IntStream of Unicode code point values from the StringBuilder object. This method is particularly useful when you need to perform stream operations on the code points of the StringBuilder.

codePoints Method Syntax

The syntax for the codePoints method is as follows:

public IntStream codePoints()

This method does not take any parameters and returns an IntStream representing the Unicode code points of the StringBuilder.

Examples

Creating an IntStream from a StringBuilder

You can use the codePoints method to create an IntStream from a StringBuilder.

Example

public class StringBuilderCodePointsExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Hello, World!");

        IntStream codePointStream = sb.codePoints();

        codePointStream.forEach(cp -> System.out.print(cp + " "));
    }
}

Output:

72 101 108 108 111 44 32 87 111 114 108 100 33

Processing Code Points with Streams

You can perform various stream operations on the code points of a StringBuilder.

Example

import java.util.stream.Collectors;

public class StringBuilderCodePointsExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Hello, World!");

        String upperCaseString = sb.codePoints()
                                   .mapToObj(cp -> (char) cp)
                                   .map(Character::toUpperCase)
                                   .map(String::valueOf)
                                   .collect(Collectors.joining());

        System.out.println(upperCaseString);
    }
}

Output:

HELLO, WORLD!

Filtering and Collecting Code Points

You can filter and collect specific code points from a StringBuilder using the codePoints method.

Example

import java.util.List;
import java.util.stream.Collectors;

public class StringBuilderCodePointsExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Hello, World!");

        List<Integer> vowels = sb.codePoints()
                                 .filter(cp -> "AEIOUaeiou".indexOf(cp) != -1)
                                 .boxed()
                                 .collect(Collectors.toList());

        System.out.println("Vowels: " + vowels);
    }
}

Output:

Vowels: [101, 111, 111]

Working with Supplementary Characters

For supplementary characters (characters outside the Basic Multilingual Plane, BMP), which are represented by a pair of char values (a surrogate pair), the codePoints method can handle them correctly.

Example

public class StringBuilderCodePointsExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Hello, World! \uD83D\uDE00"); // Unicode for 😀 (grinning face)

        sb.codePoints().forEach(cp -> System.out.print(cp + " "));
    }
}

Output:

72 101 108 108 111 44 32 87 111 114 108 100 33 32 128512

In this example, the code point for the emoji 😀 (grinning face) is correctly handled and printed as 128512.

Conclusion

The StringBuilder.codePoints() method in Java is a powerful way to obtain an IntStream of Unicode code point values from a StringBuilder object. By understanding how to use this method, you can leverage the power of Java streams to perform various operations on the code points of a StringBuilder. Whether you need to create a stream, process code points, or filter and collect specific code points, the codePoints method provides a flexible and efficient solution for these tasks.

Comments