Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 158049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:36:46+00:00 2026-05-11T10:36:46+00:00

What is the clearest way to comma-delimit a list in Java? I know several

  • 0

What is the clearest way to comma-delimit a list in Java?

I know several ways of doing it, but I’m wondering what the best way is (where ‘best’ means clearest and/or shortest, not the most efficient.

I have a list and I want to loop over it, printing each value. I want to print a comma between each item, but not after the last one (nor before the first one).

List --> Item ( , Item ) * List --> ( Item , ) * Item 

Sample solution 1:

boolean isFirst = true; for (Item i : list) {   if (isFirst) {     System.out.print(i);        // no comma     isFirst = false;   } else {     System.out.print(', '+i);   // comma   } } 

Sample solution 2 – create a sublist:

if (list.size()>0) {   System.out.print(list.get(0));   // no comma   List theRest = list.subList(1, list.size());   for (Item i : theRest) {     System.out.print(', '+i);   // comma   } } 

Sample solution 3:

  Iterator<Item> i = list.iterator();   if (i.hasNext()) {     System.out.print(i.next());     while (i.hasNext())       System.out.print(', '+i.next());   } 

These treat the first item specially; one could instead treat the last one specially.

Incidentally, here is how List toString is implemented (it’s inherited from AbstractCollection), in Java 1.6:

public String toString() {     Iterator<E> i = iterator();     if (! i.hasNext())         return '[]';      StringBuilder sb = new StringBuilder();     sb.append('[');     for (;;) {         E e = i.next();         sb.append(e == this ? '(this Collection)' : e);         if (! i.hasNext())             return sb.append(']').toString();         sb.append(', ');     } } 

It exits the loop early to avoid the comma after the last item. BTW: this is the first time I recall seeing ‘(this Collection)’; here’s code to provoke it:

List l = new LinkedList(); l.add(l); System.out.println(l); 

I welcome any solution, even if they use unexpected libraries (regexp?); and also solutions in languages other than Java (e.g. I think Python/Ruby have an intersperse function – how is that implemented?).

Clarification: by libraries, I mean the standard Java libraries. For other libraries, I consider them with other languages, and interested to know how they’re implemented.

EDIT toolkit mentioned a similar question: Last iteration of enhanced for loop in java

And another: Does the last element in a loop deserve a separate treatment?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T10:36:47+00:00Added an answer on May 11, 2026 at 10:36 am

    Java 8 and later

    Using StringJoiner class, and forEach method :

    StringJoiner joiner = new StringJoiner(","); list.forEach(item -> joiner.add(item.toString()); return joiner.toString(); 

    Using Stream, and Collectors:

    return list.stream().        map(Object::toString).        collect(Collectors.joining(",")).toString(); 

    Java 7 and earlier

    See also #285523

    String delim = ""; for (Item i : list) {     sb.append(delim).append(i);     delim = ","; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know how to do this in an ugly way, but am wondering if
What is the cleanest way to create a comma-separated list of string values from
Possible Duplicate: Clearest way to combine two lists into a map (Java)? Given this:
What is the best/cleanest/easiest way to maintain the edit history of records in Rails?
What is the best (cleanest, most efficient) way to write saturating addition in C?
The clearest way to explain this is with an image: http://i42.tinypic.com/fc1gdk.png I want to
What is the usual/clearest way to write this in Python? value, _ = func_returning_a_tuple()
given any number, what's the best way to determine it is even? how many
There are some related questions about unpacking single-value tuples, but I'd like to know
In SQL Server what is the simplest/cleanest way to make a datetime representing the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.