How to get String values ​​from an ArrayList and save them on a single line, separated by commas, in Java 8?

I have an ArrayList with some lines. I want to save this list of numbers from an ArrayList in a single line, separated by a comma, as shown below.

String s = "350000000000050287,392156486833253181,350000000000060764" 

This is my list:

  List<String> e = new ArrayList<String>(); e.add("350000000000050287"); e.add("392156486833253181"); e.add("350000000000060764"); 

I am trying to do it like this:

  StringBuilder s = new StringBuilder(); for (String id : e){ s.append(id+","); } 

The only problem is that it adds a comma to the end, and I don't want that. What would be the best way to do this?

thanks

+6
source share
7 answers

The simplest solution is to use String.join :

 List<String> list = new ArrayList<String>(); list.add("11"); list.add("22"); list.add("33"); String joined = String.join(",", list); System.out.println(joined); //prints "11,22,33" 

Please note that this requires Java 8.


However, if you want to support older versions of Java, you can fix your code with an iterator:

 StringBuilder sb = new StringBuilder(); Iterator<String> iterator = list.iterator(); // First time (no delimiter): if (iterator.hasNext()) { sb.append(iterator.next()); // Other times (with delimiter): while (iterator.hasNext()) { sb.append(","); sb.append(iterator.next()); } } 

Or just use a boolean to determine for the first time:

 StringBuilder sb = new StringBuilder(); boolean firstTime = true; for (String str : list) { if (firstTime) { firstTime = false; } else { sb.append(","); } sb.append(str); } 

But the latter should be clearly less productive than using an iterator comparing the generated bytecode for each method. However, this may not be true, as Tagir Valeev pointed out: this benchmark shows us that using the flag is more indicative with the number of iterations starting at 10.

If anyone could explain why this is so, I would be happy to know.

+18
source

Upvote for Tim for this Java 8 solution;)

If you are not using JDK 8

  StringBuilder s = new StringBuilder(); for (String id : e){ s.append(id).append(","); } String result =s.toString().replaceAll(",$", ""); 

I used regex i ",$" to detect the last comma.

And also, if you see, s.append(id +","); replaces me s.append(id +","); on s.append(id).append(","); for better performance

+7
source

You can take a boolean flag to check the first iteration, if not the first iteration add "," before adding id .

This may solve your problem.

 boolean first = true; for (String id : e){ if(!first) s.append(","); else first = false; s.append(id); } 

Note

If you are using Java8, then follow Tim's solution.

+2
source

Try iterating through the list, checking if the current index is the last value of the list (e.size-1), if it is not the last value, then connect the string as usual with the "," character, if so, then concatenate without ". "

  List<String> e = new ArrayList<String>(); e.add("3333"); e.add("4444"); e.add("3333"); StringBuilder s = new StringBuilder(); for(int i = 0; i < e.size(); i++){ if(i == e.size()-1){ s.append(e.get(i)); } else{ s.append(e.get(i)+","); } } 
+1
source
 StringBuilder s = new StringBuilder(); for(int i = 0; i < e.size()-1; i++){ s.append(e.get(i)+",") } s.append(e.get(i)) 
+1
source

Collectors.joining(",") , work well in your case, implementation example:

 import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class Solution { public static void main(String[] args) { List<String> e = new ArrayList<>(); e.add("350000000000050287"); e.add("392156486833253181"); e.add("350000000000060764"); System.out.println(e.stream().collect(Collectors.joining(","))); } } 

Output:

 350000000000050287,392156486833253181,350000000000060764 
+1
source

If we are talking about Java 8, how about:

 Collection<String> e = asList("a", "b", "c"); String result = e.stream().collect(Collectors.joining(",")); // a,b,c 

this method can be applied to any Collection strings, as well as String.join() .

0
source

All Articles