Creating the correct string representation of the collection

I often encounter the task of creating a String representation of a collection of objects.

Example:

String[] collection = {"foo", "bar", "lorem", "dolar"}; 

The view I want to create is "foo, bar, lorem, dolar" .

Every time I come across this task, I wonder which of the most convenient ways to achieve the desired result.

I know that there are many ways to get the desired view, but for example, I always wondered if it was possible to create a string just by using the for / each loop?

So:

 StringBuilder builder = new StringBuilder(); for (String tag : list) { builder.append(tag + ", "); String representation = builder.toString(); // but now the String look like this: "foo, bar, lorem, dolar, " which nobody wants 

Or is it best to use an iterator, if any, directly?

The alleged list is a collection (what they do it in the JDK):

 Iterator<String> it = list.iterator(); while (it.hasNext()) { sb.append(it.next()); if (!it.hasNext()) { break; } sb.append(", "); } 

Of course, it would be possible to index the array using the traditional for the loop or do many other things.

Question

What is the easiest / most readable / safest way to convert a list of strings to a view that separates each element with a <comma and process the edge edge at the beginning or end (does this mean that there is no comma before the first element or after the last element)?

+4
source share
8 answers

With Java 8, you can:

 String listJoined = String.join(",", list); 
+6
source

Using Java8

 String joined = Stream.of(collection) //.map(Object::toString) //for non-String elements .collect(Collectors.joining(",")); 

to get the desired result or String.join as described above.

+1
source

I think the most readable would be:

 String[] collection = {"foo", "bar", "lorem", "dolar"}; String s = Arrays.asList(collection).toString(); System.out.println(s); 

Exit

 [foo, bar, lorem, dolar] 
+1
source

You can try this way

 String[] collection = {"foo", "bar", "lorem", "dolar"}; StringBuilder sb=new StringBuilder(); for(String i:collection){ sb.append(i); sb.append(","); } sb.replace(sb.lastIndexOf(","),sb.lastIndexOf(",")+1,""); System.out.println(sb.toString()); 

Output:

 foo,bar,lorem,dolar 

In Java 8 you can try the following.

  String[] collection = {"foo", "bar", "lorem", "dolar"}; StringJoiner sj=new StringJoiner(","); for(String i:collection){ sj.add(i); } System.out.println(sj.toString()); 
0
source

You can use Apache Commons join

public static String join (Iterable iterable, char delimiter) Concatenates the elements of the provided Iterable into a single string containing the provided elements. No delimiter is added before or after the list. Zero objects or empty lines in an iteration are represented by empty lines. See examples here: join (Object [], char). Parameters: iterable - Iterable, which provides the union of values, can be a null delimiter - a separator character is used Returns: the combined String, null if the iterator input is null

It is already implemented in the popular utils package and does exactly what you do in your code.

As an alternative implementation, just for discussion, you can add a comma to your string buffer at each iteration, but skip the last character when returning a string

 Iterator<String> it = list.iterator(); while (it.hasNext()) { sb.append(it.next()); sb.append(", "); } return sb.substring(0,sb.length()-1); 
0
source

you can use the Arrays.asList function, which turns your array into a List object.

The toString function used in the list returns something like

stringCollection = "[foo, bar, lorem, dolar]"

Here you just need to remove the first and last characters ['and'] using the substring function.

result = "foo, bar, lorem, dolar"

 //This is your String array String[] collection = {"foo", "bar", "lorem", "dolar"}; //You convert it to a List and apply toString String stringCollection = Arrays.asList(collection).toString(); //Remove the '[' and ']' fromt the resulted String String result = stringCollection.substring(1, stringCollection.length()-1); 
0
source

using threads in java8:

 String[] collection = {"foo", "bar", "lorem", "dolar"}; String output = Arrays.asList(collection ).stream().collect(Collectors.joining(", ")); 
0
source

Using Apache commons lang: StringUtils.join(collection , ',');

Another similar question and answer here

0
source

All Articles