wood burning stoves 2.0*
The moose likes Performance and the fly likes Converting a String to a Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Converting a String to a Collection" Watch "Converting a String to a Collection" New topic
Author

Converting a String to a Collection

victor kamat
Ranch Hand

Joined: Jan 10, 2007
Posts: 247
I need to put the characters in String into a Set<Collection>.

One way is as follows

Set<Character> cSet = HashSet<Character>();

char[] charArray = inString.toCharArray();
for ( int i=0; i < charArray.length; i++)
cSet.add(charArray[i]);


Is there a faster way than this ?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Yes.

(But as usual with performance problems, you should probably verify that you actually have a noticeable problem, before you spend much time optimizing it.)


"I'm not back." - Bill Harding, Twister
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
There is also a *shorter* version:

cSet.addAll(Arrays.asList(inString.toCharArray()));

(Disclaimer - this is from memory, it might need slight corrections to actually compile.)

Don't know whether this will be faster or slower.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5



You don't need your chararray at all.
My code may be no faster, but its more obvious, which is more important.
Unless you know this is a problem, seems like premature optimization to me.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Pat Farrell:


You don't need your chararray at all.


On the other hand, using the array you could use the enhanced for loop:



I still think that my earlier version is more elegant...
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Originally posted by Ilja Preuss:
I still think that my earlier version is more elegant..


Your one line solution is elegant. In general, I think shorter programs are better. But sometimes, really short code is harder to maintain, since you have to grok all the cool functions. So it requires better developers.

I look at similar examples in scala, and it takes a while to figure out what the elegant code does.
victor kamat
Ranch Hand

Joined: Jan 10, 2007
Posts: 247
This wont work, gives a compile error

cSet.addAll(Arrays.asList(inString.toCharArray()));

The reason is that inString.toCharArray() returns a char[] and not Character[].
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

@victor, got it working? Yeah, a few new Character constructor calls appear to be needed, which makes the addAll() moot.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Converting a String to a Collection