Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 628
gil.fernandes Avatar asked May 18 '18 07:05

gil.fernandes


People also ask

How do you put an array into a string?

Using StringBufferCreate an empty String Buffer object. Traverse through the elements of the String array using loop. In the loop, append each element of the array to the StringBuffer object using the append() method. Finally convert the StringBuffer object to string using the toString() method.


2 Answers

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(); 
like image 183
Eugene Avatar answered Oct 20 '22 12:10

Eugene


With IntStream.mapToObj:

String s = IntStream.range(16, 30)
                    .mapToObj(String::valueOf)
                    .collect(Collectors.joining(","));
like image 36
Oleksandr Pyrohov Avatar answered Oct 20 '22 14:10

Oleksandr Pyrohov