• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2175
  • Last Modified:

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,&
0
rajannagpal
Asked:
rajannagpal
  • 4
1 Solution
 
CEHJCommented:
You can do the following if your salesCycle object implements toString properly:
0
 
CEHJCommented:

String cycles = list.toString();

Open in new window

0
 
rajannagpalAuthor Commented:
what should under toString  method of salesCycle?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
CEHJCommented:
The representation you want it to have, e.g.
0
 
CEHJCommented:
publ
public String toString() {
    return "" + id;
}

Open in new window

0
 
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

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now