AdSenseHeader

This post explains a Comma Separated String  to ArrayList using Java. I am using Java Generics for Converting  a Comma Separated String into based on your DataType  ArrayList.


Sample Java Program For Converting String into List.

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Venkata Ramu Kandulapati
 *
 */
public class ConvertToList {
      
       @SuppressWarnings("unchecked")
       public <T> List<T> toList(String comaSepString, T t) throws Exception {

              List<T> list = new ArrayList<T>();
              Constructor<?> constructor = t.getClass().getConstructor(String.class);

              for (String _s : comaSepString.split(",")) {
                     list.add((T) constructor.newInstance(_s));
              }
              return list;
       }

       public static void main(String args[]) throws Exception {
              String numberString ="1,2,3,4";
              System.out.println(new ConvertToList().toList(numberString, new Integer(0)));
             
              String comaSepString = "Windows,Ubuntu,IOS";
              System.out.println(new ConvertToList().toList(comaSepString, new String()));
       }
}

      
Output of Above Program:
[1, 2, 3, 4]
[Windows, Ubuntu, IOS]

Explanation

          In the above java Program convert the coma separated String into ArrayList.Your Coma separated String values may be String,Integer,Float or Double whatever the DataType this program is convert into ArrayList based on your Sending DataType.


13 comments:

  1. Thanks for sharing your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information.
    Regards,
    JAVA Course in Chennai|Best JAVA Training in Chennai|JAVA Training

    ReplyDelete
  2. I really love reading and following your post as I find them extremely informative and interesting. This post is equally informative as well as interesting . Java Training Institute in Chennai | DotNet Training Institute in Chennai | Web Designing Training Institute in Chennai

    ReplyDelete
  3. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.
    Digital Marketing Training in Chennai | SEO Training Institute in Chennai | Photoshop Training in Chennai

    ReplyDelete
  4. Hi, am a big follower of your blog. I am really happy to found such a helpful and fascinating post that is written in well manner. Thanks for sharing such an informative post. keep update your blog.
    No.1 Software Testing Training Institute in Chennai | Best Selenium Training Institute in Chennai | ISTQB Certification Center in Velachery

    ReplyDelete
  5. I’m really impressed with your blog article, such great & useful knowledge you mentioned here.MBA Project Center in Chennai | MBA Project in Velachery.

    ReplyDelete
  6. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training in India . Nowadays Java has tons of job opportunities on various vertical industry.

    ReplyDelete
  7. I enjoy what you guys are usually up too. This sort of clever work and coverage! Keep up the wonderful works guys I’ve added you guys to my blog roll..,

    white label website builder

    mobile website builder

    ReplyDelete
  8. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.
    aws training in Chennai

    ReplyDelete
  9. Those guidelines additionally worked to become a good way to
    recognize that other people online have the identical fervor like mine
    to grasp great deal more around this condition.

    white label website builder

    ReplyDelete
  10. Interesting post. This is really helpful for me. I like it. Thanks for sharing.
    Best IT Training Institute in Chennai |Best IT Training Institute in Velachery

    ReplyDelete
  11. I read this article. I think You put a lot of effort to create this article. I appreciate your work.
    Dissertation Writing Services

    ReplyDelete

 
Top