4

I have a comma-seperated value string in Java:

String s = "a,b,c,d";

I need to tokenize it (with comma as the delimiter) and turn it into a Set<String>. Is StringTokenizer my best bet or is there a more efficient way?

IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756

6 Answers6

7

If you try to solve generally CSV parsing to set be aware that there are quotes and coma escaping to handle. This is why libraries like OpenCSV exist. Otherwise you just need to do as hvgotcodes described.

jolivier
  • 7,380
  • 3
  • 29
  • 47
3

Try String.split(), that's probably the easiest.

    String[] a = "a,b,c,d".split( "," );
    Set<String> s = new HashSet( Arrays.asList( a ) );
Chris Kessel
  • 5,583
  • 4
  • 36
  • 55
2

Although StringTokenizer is a good option to split your input string, I personally prefer using String.split().

String[] tokens = myString.split(",");
Set<String> set = new HashSet<String>(Arrays.asList(tokens));
GETah
  • 20,922
  • 7
  • 61
  • 103
1

I would use split. split gives you an array, so

String[] toks = s.split(",")

and then

Set<String> mySet = new HashSet<String>(Arrays.asList(toks));

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
0

The Spring Framework provides StringUtils.commaDelimitedListToSet that does exactly what you're after. It's probably overkill to pull in Spring just for this, but if you're working in a framework that already includes it then it's an option to be aware of.

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
0

If you just need a simple solution without all CSV rules I would recommend StringUtils.split (instead String.split due the regex overhead):

HashSet<String> set = new HashSet<String>(Arrays.asList(StringUtils.split(text, ',')));

If you need a solution that obeys the CSV rules, you should think in use Commons CSV

Francisco Spaeth
  • 23,493
  • 7
  • 67
  • 106