Use IntStream
and StringBuilder
:
int n = 5;
String string = IntStream.rangeClosed(0, n)
.collect(StringBuilder::new, StringBuilder::append, StringBuilder::append)
.toString();
However, for this particular case it’s better to use a for-loop with StringBuilder
.
CLICK HERE to find out more related problems solutions.