Java - Write code to split string by comma

Requirements

Write code to split string by comma

return a Set<String>

Hint

use regex and String.split() method

Demo

//package com.book2s;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class Main {
    public static void main(String[] argv) {
        String values = "book2s.com ,asdfa asdf ,asdf asdf, asdf asdf ";
        System.out.println(split(values));
    }// w  w  w. ja  v  a 2s . c o  m

    public static Set<String> split(String values) {
        if (values == null || values.isEmpty()) {
            return Collections.emptySet();
        }

        HashSet<String> set = new HashSet<String>();
        Collections.addAll(set, values.split("\\s*,\\s*"));
        return set;
    }
}

Related Exercise