IAmYourFaja
IAmYourFaja

Reputation: 56914

Convert CSV to Set<String> in Java

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?

Upvotes: 4

Views: 5626

Answers (6)

Francisco Spaeth
Francisco Spaeth

Reputation: 23903

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

Upvotes: 0

Ian Roberts
Ian Roberts

Reputation: 122364

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.

Upvotes: 0

jolivier
jolivier

Reputation: 7635

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.

Upvotes: 7

GETah
GETah

Reputation: 21419

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));

Upvotes: 2

hvgotcodes
hvgotcodes

Reputation: 120188

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));

Upvotes: 1

Chris Kessel
Chris Kessel

Reputation: 5875

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

    String[] a = "a,b,c,d".split( "," );
    Set<String> s = new HashSet( Arrays.asList( a ) );

Upvotes: 3

Related Questions