What would be the most efficient way to construct a new String using a Character[]
? It's easy enough to do with the primitive char[]
, but unfortunately the String constructor does not include an option for Character[]
. Converting to a char[]
seems cumbersome. Is there any efficient way to construct the String with just the Character[]
?
Thanks.
One solution is to use a Stream
with Collectors#joining
:
Character[] chars = { 'H', 'e', 'l', 'l', 'o' };
String s = Arrays.stream(chars)
.map(Object::toString)
.collect(Collectors.joining());
System.out.println(s);
Output:
Hello
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句