I think the best way to do this is the way you're already done it in your Question using a loop. Reading the comments, it seems to be the general consensus. Nikolas Charalambidis pointed out in a comment that the solution could do with a StringBuilder
instead:
StringBuilder s = new StringBuilder();
for(int i = 0; i <= n; i++) {
s.append(i);
}
If you must use a Stream
, I believe the accepted Answer is the way to go because
- I think it just looks "clean" and
- It also uses
StringBuilder
instead of String
. There are other approaches though.
One approach is relying on side-effects. Create a variable and update it from within the stream. The JavaDoc on Package java.util.stream warns about the use:
Side-effects in behavioral parameters to stream operations are, in general, discouraged, as they can often lead to unwitting violations of the statelessness requirement, as well as other thread-safety hazards.
String[] alternativeWayA = {""};
IntStream.rangeClosed(0,n).forEach(i -> alternativeWayA[0] = alternativeWayA[0] + i);
Luiggi Mendoza mentions in one comment
You could use collect(Collectors.joining())
and have the same output
and in another comment
Well, you could use boxed
before calling collect
String alternativeWayB = IntStream.rangeClosed(0,n)
.boxed()
.map(i -> i.toString())
.collect(Collectors.joining(""));
..if one prefers using reduce
instead of Collectors.joining
:
String alternativeWayC = IntStream.rangeClosed(0,n)
.boxed()
.map(i -> i.toString())
.reduce("", String::concat);
..or as Nikolas Charalambidis suggests in this comment
you need to mapToObj
as this collector is not available for IntStream
String alternativeWayD = IntStream.rangeClosed(0,n)
.mapToObj(i -> String.valueOf(i))
.collect(Collectors.joining(""));
Holger posted a comment with
String string = new String(IntStream.rangeClosed('1', '1'+n).toArray(), 0, n);
which I think is very elegant and works for n < 10
. For n = 15
the result becomes 0123456789:;<=>?
since it's using integer values of characters. I added a + 1
to the last parameter of the String
constructor:
String alternativeWayE = new String(IntStream.rangeClosed('0', '0'+n).toArray(), 0, n + 1);