Arraylist into comma seprated list

I have an arraylist of salesCycle object  and I want to convert this arraylist into a comma separated String. For example:
Arraylist of sales Cycle and salesCycle has one of the property cycleID  I need the following result :

String cycles =salesCycle.cycleId1, salesCycle.cycleId2,&
rajannagpalAsked:
Who is Participating?
 
objectsCommented:
use the following:

public static String getSalesCycleString(List salesCyclesList) {
   StringBuilder sb = new StringBuilder();
   for (salesCycle cycle : salesCyclesList) {
      sb.append(cycle.getCycleId());
      sb.append(",");
   }
   return sb.length()==0 ? "" :  sb.substring(0, sb.length()-1);
}

0
 
CEHJCommented:
You can do the following if your salesCycle object implements toString properly:
0
 
CEHJCommented:

String cycles = list.toString();

Open in new window

0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
rajannagpalAuthor Commented:
what should under toString  method of salesCycle?
0
 
CEHJCommented:
The representation you want it to have, e.g.
0
 
CEHJCommented:
publ
public String toString() {
    return "" + id;
}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.