Solved

Arraylist into comma seprated list

Posted on 2009-04-09
6
2,120 Views
Last Modified: 2012-05-06
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
Comment
Question by:rajannagpal
  • 4
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24109820
You can do the following if your salesCycle object implements toString properly:
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24109827

String cycles = list.toString();

Open in new window

0
 

Author Comment

by:rajannagpal
ID: 24109875
what should under toString  method of salesCycle?
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 86

Expert Comment

by:CEHJ
ID: 24109946
The representation you want it to have, e.g.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24109953
publ
public String toString() {
    return "" + id;
}

Open in new window

0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 24112954
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question