Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Separator idiom

Reply
Thread Tools

Separator idiom

 
 
Roedy Green
Guest
Posts: n/a
 
      06-13-2008
Lets say you want to create a list of items separated by something
e.g. comma <BR> \n etc, but you don't want a terminator.

How do you go about constructing it with minimal fuss?
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
 
Andreas Leitgeb
Guest
Posts: n/a
 
      06-13-2008
Roedy Green <(E-Mail Removed)> wrote:
> Lets say you want to create a list of items separated by something
> e.g. comma <BR> \n etc, but you don't want a terminator.
> How do you go about constructing it with minimal fuss?


Iterator<String> it=list.iterator();
while (it.hasNext()) {
System.out.print(it.next());
if (it.hasNext()) System.out.print(sepChar);
}

Less fuss in Tcl: puts [join $list $sepChar]
 
Reply With Quote
 
 
 
 
Alexander.V.Kasatkin@gmail.com
Guest
Posts: n/a
 
      06-13-2008
On 13 , 16:20, Roedy Green <(E-Mail Removed)>
wrote:
> How do you go about constructing it with minimal fuss?


public static String build(List<?> list, String separator) {
StringBuilder buf = new StringBuilder();
for (Object obj : list) {
if (buf.length() > 0) {
buf.append(separator);
}
buf.append(obj);
}
return buf.toString();
}


BR,
Alex
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      06-13-2008
Roedy Green <(E-Mail Removed)> writes:
>Lets say you want to create a list of items separated by something
>e.g. comma <BR> \n etc, but you don't want a terminator.
>How do you go about constructing it with minimal fuss?


Because I do not know the meaning of fuss,
I will ignore with minimal fuss:

public static java.lang.CharSequence format
( final java.lang.CharSequence[] source )
{ final java.lang.StringBuilder text = new java.lang.StringBuilder();
boolean first = true;
for( final java.lang.CharSequence component : source )
{ if( first )first = false; else text.append( "\n" );
text.append( component ); }
return text; }

 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      06-13-2008
Eric Sosman <(E-Mail Removed)> writes:
> // Treat first item as "special"
> System.out.print(array[0]);
> for (int i = 1; i < array.length; ++i) {
> System.out.print(", ");
> System.out.print(array[i]);
> }
>[...] I actively try to avoid the first.


What I like about it, is that it avoids the run-time overhead
to check the position within the loop.

Two problems with this are: It can not be generalized to loops
with for( ... : ... ), and it needs an additional if to be correct:

// Treat first item as "special"
if (array.length > 0) {
System.out.print(array[0]);
for (int i = 1; i < array.length; ++i) {
System.out.print(", ");
System.out.print(array[i]);
}
}

 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      06-14-2008
Stefan Ram wrote:

>
> What I like about it, is that it avoids the run-time overhead
> to check the position within the loop.
>


It seems like one should be able to generate the sequence as normal,
then shorten the output to remove the last separator. This should be
very efficient, as reducing the size of a buffer or file is almost cost
free.


public String sequence( Object [] obj, String sep ) {

StringBuilder sb = new StringBuilder();
for( Object o : obj ) {
sb.append( o );
sb.append( sep );
}
if( sb.length() >= sep.length() ) {
sb.setLength( sb.length() - sep.length() )
}
return sb.toString();
}

But I'm not seeing a way to make that complete general (I can't find a
way to trim the size of both a StringWriter and a FileWriter. I need
some sort of random access for both. Or at least a way to change the
file length.)
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      06-14-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> On 13 , 16:20, Roedy Green <(E-Mail Removed)>
> wrote:
>> How do you go about constructing it with minimal fuss?

>
> public static String build(List<?> list, String separator) {
> StringBuilder buf = new StringBuilder();
> for (Object obj : list) {
> if (buf.length() > 0) {
> buf.append(separator);
> }
> buf.append(obj);
> }
> return buf.toString();
> }
>
>
> BR,
> Alex

get apache commons, and use StringUtils.join().

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      06-14-2008
Roedy Green wrote:
> Lets say you want to create a list of items separated by something
> e.g. comma <BR> \n etc, but you don't want a terminator.
>
> How do you go about constructing it with minimal fuss?

In JSP, I use the varStatus.last or first property.
In Java, if I can't use apache-commons-lang StringUtils, I tend to use:
final StringBuilder out = new StringBuilder();
final Iterator<Foo> it = collection.iterator();
if (it.hasNext()) {
out.append(it.next());
}
while (it.hasNext()) {
out.append(separator);
out.append(it.next());
}

Or, if I'm not concerned with efficiency, which is usually the case:
final StringBuilder out = new StringBuilder();
for (Foo foo: collection) {
if (out.size() != 0) {
out.append(separator);
}
out.append(foo);
}

All of these are pretty standard idioms. Unless you use a utility
method somewhere, I don't think you'll get much smaller than that.

In python I use ", ".join(myStrings) , although I never use python.


--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      06-16-2008
On Fri, 13 Jun 2008 12:20:55 GMT, Roedy Green
<(E-Mail Removed)> wrote, quoted or indirectly quoted
someone who said :

>Lets say you want to create a list of items separated by something
>e.g. comma <BR> \n etc, but you don't want a terminator.
>
>How do you go about constructing it with minimal fuss?
>--


I have taken your various suggestions and canonicalised them into 7
methods that all use the same parms and naming conventions. They are
posted at http://mindprod.com/jgloss/separator.html
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      06-20-2008
On Thu, 19 Jun 2008 01:44:43 +0200, Piotr Kobzda <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>Note that join3() for some input may give different result than the
>other methods, for example:
>
>List<String> input = Arrays.asList( "", "X", "" );
>
>join2( input, "|" ) --> "|X|"
>join3( input, "|" ) --> "X|"
>
>
>Note also, that join1() will fail for zero-element list and non-empty
>separator input. To correct it trim off the last separator
>only when sb.length() > 0.
>
>
>Oh, and if you're still collecting them, here is another approach:
>
>public static String join(String[] a, String sep) {
> StringBuilder sb = new StringBuilder();
> if (a.length > 0) {
> for (int i = 0; {
> sb.append(a[i]);
> if (++i == a.length) break;
> sb.append(sep);
> }
> }
> return sb.toString();
>}


I added your method, canonicalised to use the same parms as the
others.

I corrected one of the flaws you discovered. If I corrected the other
flaw it turns into one of the other methods, so I left it as is, just
documented it.

I added two tests to the harness to check your corner cases..

You can see the results at http://mindprod.com/jgloss/separator.html
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
String format, thousand separator and decimals hansiman ASP .Net 1 10-08-2004 07:29 PM
Compare validator does not allow thousand separator for double datatype. Kaja ASP .Net 0 09-09-2004 05:28 AM
need url parameter separator "&" not "&amp;" DC Gringo ASP .Net 3 08-19-2004 08:39 PM
Help with Perl newbie..the $/ separator Javier Perl 5 01-16-2004 02:21 AM
Re: Separator in panel Kevin Spencer ASP .Net 0 08-20-2003 03:45 PM



Advertisments