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
String -> Collection conversion: (String -> String[] -> Collection)
// java version 8
String str = "aa,bb,cc,dd,aa,ss,bb,ee,aa,zz,dd,ff,hh";
// Collection,
// Set , List,
// HashSet , ArrayList ...
// (____________________________)
// || ||
// \/ \/
Collection col = new HashSet<>(Stream.of(str.split(",")).collect(Collectors.toList()));
Collection -> String[] conversion:
String[] se = col.toArray(new String[col.size()]);
String -> String[] conversion:
String[] strArr = str.split(",");
And Collection -> Collection:
List list = new LinkedList<>(col);