Java ArrayList StringBuilder appending -


i have 1 problem when appending list values string. getting 1 or more 1 value list. need append these string 1 string , use separator , each string. @ end comma getting added. how can remove @ end of string dynamically. here code:

cntlist = parsexml(metadata.xml); if (cntlist.size() != 0) {     // entrycountrymap.put(id, country);     system.out.println("size  ---->" + cntlist.size());     stringbuilder sb = new stringbuilder();     if (cntlist.size() >= 2) {         (string s : cntlist) {             sb.append(s);             sb.append(",");         }     }     system.out.println("stringbuilder ----->" + sb.tostring()); } 

and output this:

stringbuilder ----->all countries,us - united states, 

please me resolving this. - raji

with java 8 it's 1 liner

string.join(",", cntlist); 

if need filter elements of list and/or mutate strings before joining single string, can still in concise manner without loops.

cntlist.stream().filter(...).map(...).collect(collectors.joining(",")); 

(the ... need replaced filtering predicate , mapper)


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -