How to convert a range to a delimited string in Java 8+

  • A+
Category:Languages

How can you convert a range in Java (either using java.util.stream.LongStream or java.util.stream.IntStream) to a delimited string in Java?

I have tried:

String str = LongStream.range(16, 30)                 .boxed()                 .map(String::valueOf)                 .collect(Collectors.joining(",")); System.out.println(str); 

This prints:

16,17,18,19,20,21,22,23,24,25,26,27,28,29 

The same can be used with IntStream. Is there a more convenient conversion of a range to a delimited string?


Seriously, just for the fun of it. Using guava:

String result = ContiguousSet.create(                        Range.closedOpen(16, 31), DiscreteDomain.integers())                              .asList()                              .toString(); 

Or

 String result = String.join(",",             IntStream.rangeClosed(16, 30).mapToObj(String::valueOf).toArray(String[]::new)); 

Or:

String result = String.join(",",             () -> IntStream.rangeClosed(16, 31).mapToObj(x -> (CharSequence) String.valueOf(x)).iterator()); 

Or (seems like I got carried away a bit with this):

String result = IntStream.rangeClosed(16, 31)             .boxed()             .collect(                     Collector.of(                             () -> new Object() {                                 StringBuilder sb = new StringBuilder();                             },                             (obj, i) -> obj.sb.append(i).append(",")                             ,                             (left, right) -> {                                 left.sb.append(right.sb.toString());                                 return left;                             },                             x -> {                                 x.sb.setLength(x.sb.length() - 1);                                 return x.sb.toString();                             })             ); 

And after Holger's good points, here is even a simpler version:

    StringBuilder sb = IntStream.range(16, 30)             .collect(                     StringBuilder::new,                     (builder, i) -> builder.append(i).append(", "),                     StringBuilder::append);      if (sb.length() != 0) {         sb.setLength(sb.length() - 2);     }     String result = sb.toString();  

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: