codePoints Stream Walks Each Character

Stream a string as int code points with String.codePoints, so per-character logic counts one emoji as a single value instead of two surrogate units.

JavaData Integrity

Code

snippet
public class PassportCheck {

    public static void main(String[] args) {
        String plate = "AB🚀12";

        // chars() 按 UTF-16 代码单元遍历:🚀 被拆成两个孤立的代理单元,
        // 数出来是 6;codePoints() 还原成 int 码点,数出来是 5
        System.out.println("char units: " + plate.chars().count());
        System.out.println("code points: " + plate.codePoints().count());

        // 按码点分派:字母、数字、其他各走各的分支
        long letters = plate.codePoints().filter(Character::isLetter).count();
        long digits = plate.codePoints().filter(Character::isDigit).count();
        long others = plate.codePoints().count() - letters - digits;
        System.out.println("letters=" + letters + " digits=" + digits + " others=" + others);

        // 收集回字符串:appendCodePoint 保持码点完整,不会截断代理对
        String digitsOnly = plate.codePoints()
                .filter(Character::isDigit)
                .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
                .toString();
        System.out.println("digits only: " + digitsOnly);
    }
}

How it works

codePoints Stream Walks Each Character comes down to the class PassportCheck and the function main.

Caveats

  • No dependencies section needed; the standard library is the whole story.

Using it

The primary signature is public static void main(String[] args)

The run prints char units:, code points:, letters= — each prefix names the step that produced it.

Key statements

long letters = plate.codePoints().filter(Character::isLetter).count();
long digits = plate.codePoints().filter(Character::isDigit).count();
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();

Interactive Tools for This Topic

FAQ

What does codePoints Stream Walks Each Character print when run?
Running the file directly is enough — expect labeled output lines such as `char units:`, `code points:`, `letters=`.