If I have a List< Integer > whose integer values are Unicode code point numbers. How do I construct a String object of characters determined by those code points?
For example:
List < Integer > codePoints = List.of( 100, 111, 103, 128054 ) ;
… or:
List < Integer > codePoints = "cat".codePoints().boxed().toList();
How do I get another String object with value cat from codePoints?
List➠Stream➠StringBuilder➠StringOne solution is to convert your
Listinto aStream. Then collect the elements of that stream into aStringBuilder. TheStringBuilderclass offers anappendCodePointmethod specifically to accommodate code point integer numbers. When the mutableStringBuilderis complete, convert to an immutableString.Or different formatting:
Here is some example code.
See this code run live at IdeOne.com.
To understand how that code with
StringBuildermethod references works, see Java 8 Int Stream collect with StringBuilder.We could make a utility method of this code, for convenience. For safety, we could add a call to
.filterto skip any invalid code point number (either negative or beyondCharacter.MAX_CODE_POINT).See that code run live at IdeOne.com.