Tuesday, May 10, 2016

How to convert comma-separated String to ArrayList?

Here is another one for converting CSV to ArrayList:
List<PageData> pages = new ArrayList<PageData>();
                
if (data.getPageName() != null) {
   String pageNames=data.getPageName();
   String pageIds=data.getPageId();
   List nameList= new ArrayList(Arrays.asList(pageNames.split(",")));
   List idList= new ArrayList(Arrays.asList(pageIds.split(",")));
                    
   for(int i=0;i<nameList.size();i++){
                        
      long layoutId = Long.valueOf(idList.get(i).toString());
      PageData pD = new PageData();
                        
      pD.setName(nameList.get(i).toString());
      pD.setLayoutId(layoutId);

      pages.add(pD);
   }
}