I tried to filter a stream of characters based on some constraints and then tried to collect them in a string.
This is what I tried:
String s = str.chars()
.mapToObj(c -> (char) c)
.map(Character::toLowerCase)
.filter(c -> !Character.isWhitespace(c))
.collect(Collectors.joining());
Collectors.joining()
should concatenate the contents of the stream into a String, but this last bit of code produces an error. Why is this happening?
You need to convert the Character to String before joining, you need this:
...
.map(c -> Character.toString(c))
.collect(Collectors.joining());