public static String getCommaSeparated(List<Long> list) { String s = ""; for (Long v : list) { if (s.length() > 0) s += ", "; s += v; } return s; } 
  • Optimize for what? I would suggest keeping the Boolean variable instead of length () at each iteration of the cycle ... - Vladimir Martyanov
  • one
    String s = list.toString (); return s.substring (1, s.length () - 1) will produce exactly the same result - Andrew Bystrov
  • Without substringing there will be at the beginning [and at the end] - Andrew Bystrov
  • @AndrewBystrov exactly, I forgot - Alexey Shimansky

3 answers 3

For concatenation in a loop, use StringBuilder

 StringBuilder sb = new StringBuilder(); String delim = ""; for (Long aLong : list) { sb.append(delim).append(aLong); delim = ", "; } return sb; 

for Java 8, you can use Collectors

 return list.stream().map(Object::toString).collect(Collectors.joining(", ")); 

Instead of Object::toString you can use lambda el -> el.toString() (to taste), ie:

 return list.stream().map(el -> el.toString()).collect(joining(", ")); 

where for joining java.util.stream.Collectors.joining; imported java.util.stream.Collectors.joining;

  • one
    And why is it better to use lambda than a reference to a method? - Andrew Bystrov
  • @AndrewBystrov Well, of course this is my IMHO. more clearly shows what is happening. But probably it is to your taste. Removed "even better" so as not to embarrass - Alexey Shimansky
  • It’s just that el -> el.toString () constructs a little idea for me to change to :: toString and I’m writing this on the machine) - Andrew Bystrov
  • @AndrewBystrov well, not everything that she proposes to change there is good)) Anyway, it’s probably a matter of taste. Although there may be some millimeter difference in speed. But I do not know this, to be honest. - Alexey Shimansky

This is a string concatenation in java.

Optimization will look like this:

 public static String getCommaSeparated(List<Long> list) { StringBuilder builder = new StringBuilder(); for (Long v : list) { if (builder.length() > 0) builder.append(", "); builder.append(v); } return builder.toString(); } 

More details can be found, for example, here: https://habrahabr.ru/post/260767/

  • Do not reinvent the wheel. See the implementation of the toString () method in the java.util.AbstractCollection class. There will be almost the same thing, except that there will be added [and] to the beginning and end respectively - Andrew Bystrov
  • @AndrewBystrov well, so what’s with bikes. They asked about a specific method - this is its optimization. What is written here is almost the same as in java.util.AbstractCollection :: toString () only confirms that the optimization is correct. - Kirill Akhmetov
  • @AndrewBystrov so we end up with the same + delete task [at the beginning and] at the end. Good optimization. Keep it up! - Sergey

For the task of splitting a list into a string separated by some kind of sign, you can use the method StringUtils.join(list, ","); from the library org.apache.commons.lang3.StringUtils which returns the desired String

  • The fact is that there are not strings, but Long . I also overlooked at the beginning .... And so it will be ClassCastException cannot be cast to java.lang.String ..... and the comma should be written in double quotes - Alexey Shimansky
  • At the expense of the quotation marks, yes, but at the expense of ClassCastException - no, inside this method toString is called, and Long without problems will get through in this method - Werder