Converting int array to char array
Yeah, we’re missing a stream method to produce a char array. Maybe a whole CharStream class. In any case, no, you cannot cast between int[]
and char[]
.
In the meantime, it’s getting a long line, but it works:
return IntStream.rangeClosed('a', 'z')
.mapToObj(c -> Character.toString((char) c))
.collect(Collectors.joining())
.toCharArray();
This gives a char[]
containing
[a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z]
From Java-11 and onwards , you can use .mapToObj(Character::toString)
instead of .mapToObj(c -> Character.toString((char) c))
, so your overall code boils down to :
return IntStream.rangeClosed('a', 'z')
.mapToObj(Character::toString)
.collect(Collectors.joining())
.toCharArray();