DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Join In Java

04.09.2005
| 149053 views |
  • submit to reddit
        Join a collection of objects into a String using the specified delimiter.  One thing to note is that the collection can contain any object.  The object's toString() method will be used to convert said object to its String representation.

    public static String join(Collection s, String delimiter) {
        StringBuffer buffer = new StringBuffer();
        Iterator iter = s.iterator();
        while (iter.hasNext()) {
            buffer.append(iter.next());
            if (iter.hasNext()) {
                buffer.append(delimiter);
            }
        }
        return buffer.toString();
    }
    

Comments

Constration Security replied on Wed, 2014/02/12 - 12:39pm

Hi, Its very interesting and informative blog, thanks for sharingRegards, Hire Domestic Workers in London


follow us : Hire Domestic Workers in London 

Constration Security replied on Wed, 2014/02/12 - 12:18pm

Hello everyone, This blog is very helpfuln have great information , thanks for sharingRegards, Constration Security London

Follow Us: construction site security in london 


Constration Security replied on Wed, 2014/02/12 - 2:18pm


Hello World, Really appreciate the blog and sensible comments , keep sharingregards, Ibody Gaurdbodyguard services London follow us :  security companies in london 

Constration Security replied on Wed, 2014/02/12 - 12:36pm

Hi, This is great informative and helpful blog , thanks for sharingRegards, Ievent Securityfollow us : Hire security in london 

Snippets Manager replied on Tue, 2010/03/23 - 9:33am

You can also have a look at import org.apache.commons.lang.StringUtils; StringUtils.join(...) http://www.java2s.com/Code/JavaAPI/org.apache.commons.lang/StringUtilsjoinObjectarrayStringseparator.htm

Snippets Manager replied on Fri, 2010/04/16 - 10:39am

I have found that preallocating the buffer with the correct capacity is 2-5% faster on average. private static String join(List<? extends CharSequence> s, String delimiter) { int capacity = 0; int delimLength = delimiter.length(); Iterator<? extends CharSequence> iter = s.iterator(); if (iter.hasNext()) { capacity += iter.next().length() + delimLength; } StringBuilder buffer = new StringBuilder(capacity); iter = s.iterator(); if (iter.hasNext()) { buffer.append(iter.next()); while (iter.hasNext()) { buffer.append(delimiter); buffer.append(iter.next()); } } return buffer.toString(); }

Snippets Manager replied on Wed, 2009/11/18 - 2:22pm

The last Iterable lacks the null check so combining them together with the "hasNext()" And of course we should use the StringBuilder instead of the Threadsafe but slower StringBuffer. public static String join( Iterable< ? extends Object > pColl, String separator ) { Iterator< ? extends Object > oIter; if ( pColl == null || ( !( oIter = pColl.iterator() ).hasNext() ) ) return ""; StringBuilder oBuilder = new StringBuilder( String.valueOf( oIter.next() ) ); while ( oIter.hasNext() ) oBuilder.append( separator ).append( oIter.next() ); return oBuilder.toString(); }

Snippets Manager replied on Tue, 2007/06/05 - 11:43am

This will also work correctly, if the Collection has nulls, or does not implements the protocol correctly. public static String join(final Collection objs, final String delimiter) { if (objs == null || objs.isEmpty()) return ""; Iterator iter = objs.iterator(); // remove the following two lines, if you expect the Collection will behave well if (!iter.hasNext()) return ""; StringBuffer buffer = new StringBuffer(String.valueOf(iter.next())); while (iter.hasNext()) buffer.append(delimiter).append(String.valueOf(iter.next())); return buffer.toString(); } Also it could be implemented on Iterables, not just Collections public static String join(final Iterable objs, final String delimiter) { Iterator iter = objs.iterator(); if (!iter.hasNext()) return ""; StringBuffer buffer = new StringBuffer(String.valueOf(iter.next())); while (iter.hasNext()) buffer.append(delimiter).append(String.valueOf(iter.next())); return buffer.toString(); }

Snippets Manager replied on Fri, 2008/10/10 - 2:53pm

public static String join(final Collection objs, final String delimiter) { if (objs == null || objs.isEmpty()) return ""; Iterator iter = objs.iterator(); StringBuffer buffer = new StringBuffer(iter.next().toString()); while (iter.hasNext()) buffer.append(delimiter).append(iter.next().toString()); return buffer.toString(); }

Snippets Manager replied on Fri, 2008/10/10 - 2:53pm

public static String join(final Collection objs, final String delimiter) { if (objs == null || objs.isEmpty()) return ""; Iterator iter = objs.iterator(); StringBuffer buffer = new StringBuffer(iter.next().toString()); while (iter.hasNext()) buffer.append(delimiter).append(iter.next().toString()); return buffer.toString(); }

Snippets Manager replied on Tue, 2008/07/29 - 4:46pm

And we can leverage the fact that append is a telescoped operator and compose them: public static String join(AbstractCollection s, String delimiter) { if (s.isEmpty()) return ""; Iterator iter = s.iterator(); StringBuffer buffer = new StringBuffer(iter.next()); while (iter.hasNext()) buffer.append(delimiter).append(iter.next()); return buffer.toString(); }

Snippets Manager replied on Tue, 2008/07/29 - 4:46pm

Let's not create the StringBuffer/Iterator unless we need to: public static String join(AbstractCollection s, String delimiter) { if (s.isEmpty()) return ""; Iterator iter = s.iterator(); StringBuffer buffer = new StringBuffer(iter.next()); while (iter.hasNext()) { buffer.append(delimiter); buffer.append(iter.next()); } return buffer.toString(); }

Snippets Manager replied on Tue, 2008/01/15 - 3:51am

We can make it faster by taking the 'if' out of the loop: public static String join(AbstractCollection s, String delimiter) { StringBuffer buffer = new StringBuffer(); Iterator iter = s.iterator(); if (iter.hasNext()) { buffer.append(iter.next()); while (iter.hasNext()) { buffer.append(delimiter); buffer.append(iter.next()); } } return buffer.toString(); }

Snippets Manager replied on Thu, 2006/04/06 - 7:48pm

hey whats up everyone write back