Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Converting a String to a Collection

 
victor kamat
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@victor, got it working? Yeah, a few new Character constructor calls appear to be needed, which makes the addAll() moot.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic