Is there any built-in method in Java which allows us to convert comma separated String to some container (e.g array, List or Vector)? Or do I need to write custom code for t
I usually use precompiled pattern for the list. And also this is slightly more universal since it can consider brackets which follows some of the listToString expressions.
private static final Pattern listAsString = Pattern.compile("^\\[?([^\\[\\]]*)\\]?$");
private List getList(String value) {
Matcher matcher = listAsString.matcher((String) value);
if (matcher.matches()) {
String[] split = matcher.group(matcher.groupCount()).split("\\s*,\\s*");
return new ArrayList<>(Arrays.asList(split));
}
return Collections.emptyList();