• Post Reply Bookmark Topic Watch Topic
  • New Topic

Alternative to com.google.common.base.Joiner ?  RSS feed

 
S Chan
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
com.google.common.base.Joiner is a great class when you want to join a list into a string. It is simple to use and used by many.
For those who haven't heard of it before, here are some examples:

It's so much easier than writing a custom loop and join the string together. At least, this saves you the trouble to decide when to add a comma:


I have been enjoying this class/lib all the time until I met a problem today. I have a list of objects. I want to join them into String like above. However, I don't want to use toString() this time. I want to get a member field of each element and join them, e.g.:
If I want to join all the lastname of each element of the list, with comma separated, I can't make good use of the Joiner class.
I know I can write a custom loop like above, but I was thinking is there really a more elegant way of doing it.

Comments are welcomed.

Thank you all in advanced!
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I want to join all the lastname of each element of the list, with comma separated, I can't make good use of the Joiner class.

You can, if you provide an overridden toString() to MyClass which will return the last name.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S Chan wrote:

Actually you can do this in an easier manner. ArrayList#toString prints[obj(0).toString(),obj(1).toString()...obj(n).toString()]
You can take out the [ and ] by using String#substring(int startIndex,int endIndex)
 
S Chan
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:
If I want to join all the lastname of each element of the list, with comma separated, I can't make good use of the Joiner class.

You can, if you provide an overridden toString() to MyClass which will return the last name.


Thank you for the reply.

Yes, you are right. I could override toString(), but unfortunately that is not what I want.

Overriding toString() to carry out a specific task would be kind of inappropriate in general case. toString() is preferably kept as generic as possible, especially for logging purposes for most people.

Let's say if I now want two strings, one from the concatenated firstnames and one from the lastnames. Then overriding would only let me to achieve one, not both.
 
S Chan
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:
S Chan wrote:

Actually you can do this in an easier manner. ArrayList#toString prints[obj(0).toString(),obj(1).toString()...obj(n).toString()]
You can take out the [ and ] by using String#substring(int startIndex,int endIndex)


Well, that's quite a clever hack, but rather inflexible.

That's limited to ArrayList's toString implementation. If I now want the separator be hyphens (-), then I am stuck again...
 
bhanu chowdary
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S Chan wrote:That's limited to ArrayList's toString implementation. If I now want the separator be hyphens (-), then I am stuck again...

You can use String's replace method in that case.
 
S Chan
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bhanu chowdary wrote:
S Chan wrote:That's limited to ArrayList's toString implementation. If I now want the separator be hyphens (-), then I am stuck again...

You can use String's replace method in that case.


Hmmm... Yes and no. Not completely fool proof though... If there are commas in the original String, a blind String replace would mess up the original String
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can apply any operations you want to the list elements, without having to modify any code in MyClass:

As usual, this will probably look much nicer if Java does eventually get lambdas in JDK 8 or whenever. Perhaps something like this:
 
S Chan
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:You can apply any operations you want to the list elements, without having to modify any code in MyClass:

As usual, this will probably look much nicer if Java does eventually get lambdas in JDK 8 or whenever. Perhaps something like this:


Thank you. That seems to be the tidiest method I have seen so far.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!