Javatip : inserting commas while iterating collection

Many times when we need to display a collection ie array, list, set, vector to the end user, we have to insert comma between the elements. There are several conditions to satisfy before really inserting comma,

1. There should be more then 1 value in the collection.

2. There should not be any comma after the last value.

3. Only intermediate values should have commas.

4. If printing arrays, there must not be any bracket.

 

The optimized solution for set is

image

The optimized solution for list/array is

image

One thought on “Javatip : inserting commas while iterating collection

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s