how to convert list object into a string in java

Hi Experts,

I Want to get the list of names from the database
List<Object>names= ///getting from database
now i want convert this to String .
i have written the below code.
is there any to cut down my code.instead of using multiple for loops.
Thanks,

public String getNames(){
List<Object>names = dao.getDataFromDatabase()
for(Object object:names){
Object[] row = (Object[])object;
List<String>al = new ArrayList<>();
al.add((String) row[0]);
}
return convertlistToString(List<String>al)
}

Public String convertlistToString(List<String>al){
	StringBuilder commaSepValueBuilder = new StringBuilder();
    for ( int i = 0; i< al.size(); i++){
      //append the value into the builder
      commaSepValueBuilder.append(al.get(i));
       
      //if the value is not the last element of the list
      //then append the comma(,) as well
      if ( i != al.size()-1){
        commaSepValueBuilder.append(", ");
      }
    }
    commaSepValueBuilder.toString();

}

Open in new window

LVL 2
srikoteshAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gurpsbassiCommented:
public String convertlistToString(final List<String> al) {
        return al.toString().replaceAll("\\[|\\]", "");
    }

Open in new window



Although I personally would not use this shortcut. I would prefer to build the string up myself.
CEHJCommented:
return al.toString().replaceAll("\\[|\\]", "");

Open in new window


That's ok, but better if optimized to

return al.toString().replaceAll("^\\[|\\]$", "");

Open in new window

srikoteshAuthor Commented:
Hi Experts,

i am getting o/p along with array.
[sri, nivas, rao]
i dont want array here
i need output like
sri, nivas, rao
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

gurpsbassiCommented:
Have you tried the solution?
srikoteshAuthor Commented:
no
srikoteshAuthor Commented:
yes i am getting the expected response.

List<String> ids = new ArrayList<String>();
	ids.add("sri");
	ids.add("ni");
	ids.add("vas");
	ids.add("rap");

	// CSV format
	String csv = ids.toString().replace("[", "").replace("]", "")
	            .replace(", ", ",");
	System.out.println("csv is "+csv);

Open in new window


o/p:
sri,ni,vas,rap
srikoteshAuthor Commented:
String csv = ids.toString().replace("[", "").replace("]", "");
CEHJCommented:
There's no reason to change the solution that gurpsbassi and i showed
srikoteshAuthor Commented:
Hi CEHJ,

i have verified that, list is coming with array

return al.toString().replaceAll("^\\[|\\]$", "");

[sri, ni, vas, rao, rrrr, rsss]

can u verify once how to modify this statement.
CEHJCommented:
You're wrong. That's not how it displays:

import java.util.*;


public class Rep {
    public static void main(String[] args) {
        System.out.println(Rep.test());
    }

    public static String test() {
        List<String> al = Arrays.asList(new String[] { "A", "B", "C" });

        return al.toString().replaceAll("^\\[|\\]$", "");
    }
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
srikoteshAuthor Commented:
Excellent
CEHJCommented:
Answer by gurpsbassi really
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.