Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: for :each style question Date: Wed, 14 Dec 2011 16:53:41 -0800 (PST) Organization: http://groups.google.com Lines: 47 Message-ID: <31677245.18.1323910421101.JavaMail.geo-discussion-forums@yqnd19> References: <30fdd7tlkkejm29ufduhc9nnfk7uu3c6h0@4ax.com> <2pehw6alhc.fsf@shell.xmission.com> Reply-To: comp.lang.java.programmer@googlegroups.com NNTP-Posting-Host: 173.164.137.214 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: posting.google.com 1323910536 19624 127.0.0.1 (15 Dec 2011 00:55:36 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 15 Dec 2011 00:55:36 +0000 (UTC) In-Reply-To: <2pehw6alhc.fsf@shell.xmission.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=173.164.137.214; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T User-Agent: G2/1.0 X-Google-Web-Client: true Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:10759 Jim Janney wrote: > Wanja Gayk writes: >> newsgrou...@virtualinfinity.net says... >> >>> I find specific use-cases call for different approaches. For instance, >>> I like the following for string concatenation: >>> >>> final StringBuilder builder = new StringBuilder(); >>> >>> String sep = ""; >>> >>> for (Object o: objects) { >>> builder.append(sep).append(o); >>> sep = ", "; >>> } >> >> I always preferred to do it this way: >> >> final StringBuilder builder = new StringBuilder(); >> final String sep = ", "; >> for (final Object o: objects) { >> builder.append(o).append(sep); >> } >> builder.setLength(Math.max(0,builder.length()-sep.length())); >> >> The only drawback is that the size of the builder's internal buffer may >> grow unnecessarily, if the last separator crosses its current bounds, >> but how often does that happen and how often is it really a problem? > > import org.apache.commons.lang.StringUtils; > > ... > > String joinedObjects = StringUtils.join(objects, ", "); > > Not to go all Lew on you here, You flatter me, sir. > but the Apache Commons stuff is worth a second look. ... and a third, and a fourth, ... That said, there are things in Commons that I would not, or not any longer, use, necessarily. You don't want to use a hammer to unscrew a rivet. But it is good to have a hammer in your toolkit when you need to pound in a nail. -- Lew