Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

splitting a large string into SET items [duplicate]

Tags:

java

set

Possible Duplicate:
Java - easily convert array to set

Can someone help me with a version of the following expression that I can use for SET instead of ArrayList ?

ArrayList<String> items = new ArrayList<String>(Arrays.asList(comment.split(", ")));

P.S.: Comment is a large string of words split with a ",". Need to make an individual item of the word by splitting them from the comma sections.

like image 327
Achilles Avatar asked Jul 09 '12 00:07

Achilles


1 Answers

You use the same approach, just passing the converted array to the constructor of a Set implementation:

Set<String> items = new HashSet<String>(Arrays.asList(comment.split(", ")));

Further simplification are not possible without third-party libraries, but there are no drawbacks, since Arrays.asList executes in constant time O(1).

like image 131
Konrad Reiche Avatar answered Sep 20 '22 20:09

Konrad Reiche