Thursday, 18 July 2013

ArrayList to comma separated strings And comma separated strings to an ArrayList.

                

               /**         * Convert Array List to String Comma              *separated         */


              ArrayList<String> list = new ArrayList<String>();

              list.add("one");

              list.add("two");
              list.add("three");

              StringBuilder sb = new StringBuilder();
              for (String s : list) {
                     sb.append(s);
                     if (sb.length() > 0)
                           sb.append(",");
              }
              String returnedItems = sb.toString();
              System.out.println(returnedItems);
              //OUT PUT one,two,three
            
      /**
       * Convert String Comma separated elements          * into List
       */

              List<String> sellItems = Arrays.asList(returnedItems.split(","));
              System.out.println(sellItems.toString());

              //OUT PUT [one,two,three]