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
List<String> items = Arrays.asList(commaSeparated.split(","));
That should work for you.
ArrayList<HashMap<String, String>> mListmain = new ArrayList<HashMap<String, String>>();
String marray[]= mListmain.split(",");
You can first split them using String.split(",")
, and then convert the returned String array
to an ArrayList
using Arrays.asList(array)
Two steps:
String [] items = commaSeparated.split("\\s*,\\s*");
List<String> container = Arrays.asList(items);
You can do it as follows.
This removes white space and split by comma where you do not need to worry about white spaces.
String myString= "A, B, C, D";
//Remove whitespace and split by comma
List<String> finalString= Arrays.asList(myString.split("\\s*,\\s*"));
System.out.println(finalString);
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<String> 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<String> list = new LinkedList<>(col);