• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get data from Set in one go

 
Bhagat Singh Rawat
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

I want to get all values of a Set interface in one go in Comma Separated String.

For Example:

if I print Set as value.toString then the output would be:

But my requirement is
Apple, Banana, Orange
without [] squire brackets.

Please suggest.
 
armando fonseca
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is this a homework? or an assignment ? there is a policy about question regarding those scenario. You can use String methods, or regex if you are comfortable with it.
 
Bhagat Singh Rawat
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
armando fonseca wrote:is this a homework? or an assignment ? there is a policy about question regarding those scenario. You can use String methods, or regex if you are comfortable with it.


Its homework in terms of performance, I have 3 solutions:
1-

2-

fruits.toString().substring(1, fruits.toString().length - 1);

3-
String allFruits = StringUtils.join(fruits, ", ");
Note: StringUtils is class from Apache Commons Lang Library

Now you tell me which one is good in terms of performance?
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hard to tell unless you actually clock it. Any algorithm would take atleast O(N) time.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bhagat Singh Rawat wrote:Its homework in terms of performance, I have 3 solutions: ...

There is really only one way to do this: go through all the values in the set and concatenate them together. All three of your solutions come down to the same, there will not be a major performance difference between them. I bet that if you look up the source code of the StringUtils method from Apache Commons, it will be very similar to your first solution.

Performance-wise, you're already doing the right thing in your own first solution in that you are using a StringBuilder to concatenate all the parts together, instead of using String objects and concatenating them with the + operator (that would make a lot of unnecessary temporary String objects and unnecessary copying of data).

By the way, this is not really a question about the SCJP. I will move this topic to a more appropriate forum for you.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that there's a very easy way to double the performance of method #2 -- but I'll leave that to your imagination...
 
Rob Spoor
Sheriff
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bhagat Singh Rawat wrote:1-

That's more or less what Set's toString does (or actually AbstractCollection):
I agree that using your while code looks much nicer than this though.

And to quote Campbell: never use "== true" or "== false". You risk using an assignment if you omit one of the = symbols.
 
Bhagat Singh Rawat
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:Note that there's a very easy way to double the performance of method #2 -- but I'll leave that to your imagination...


Humm, I think that is out of reach . Please tell me how can I do that.
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would imagine is has something to do with you calling "fruits.toString()" 2 times in the same method call. A doubling of performance would occur if you could somehow omit one
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wow... took me 4 tries to post that. The boards are really acting up today
 
Bhagat Singh Rawat
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Legg wrote:I would imagine is has something to do with you calling "fruits.toString()" 2 times in the same method call. A doubling of performance would occur if you could somehow omit one


You mean if I change my code as

would be faster than the earlier code?


 
David Marco
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fruits.toString().substring(1, fruits.toString().length - 1);

above code appears to create three string objects "from side to side" whereas

1. String str = fruits.toString();
2. str = str.substring(1, str.length - 1);

creates two Strings. Also the first solution leaves all the three objects unreferenced on the heap (a memory wast?) whereas the last solution only leaves one unreferenced String.

Correct me if I'm wrong.
 
Rob Spoor
Sheriff
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Marco wrote:
fruits.toString().substring(1, fruits.toString().length - 1);

above code appears to create three string objects "from side to side" whereas

1. String str = fruits.toString();
2. str = str.substring(1, str.length - 1);

creates two Strings. Also the first solution leaves all the three objects unreferenced on the heap (a memory wast?) whereas the last solution only leaves one unreferenced String.

Correct me if I'm wrong.

Technically you are right. However, the String returned by String.substring shares the string data (a char[]) with the original String. So yes, there is a little "waist", but that amounts to one object reference with a few primitive fields. Nothing to worry about unless resources are scarce.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic