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.


21 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

  12. Hello, I read your blog occasionally, and I own a similar one, and I was just wondering if you get a lot of spam remarks? If so how do you stop it, any plugin or anything you can advise? I get so much lately it’s driving me insane, so any assistance is very much appreciated.
    AWS Training in Chennai | Best AWS Training in Chennai
    Best Data Science Training in Chennai
    Best Python Training in Chennai
    Best RPA Training in Chennai
    Digital Marketing Training in Chennai
    Matlab Training in Chennai
    Best AWS Course Training in Chennai
    Best Devops Course Training in Chennai
    Java Training Institute in Chennai
    C C++ Training in Chennai

    ReplyDelete
  13. This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
    Big Data Analytics Courses in Chennai
    Big Data Analytics Training in Chennai
    Python Training in Chennai
    Java Training in Chennai
    Software Testing Training in Chennai
    Big Data Analytics Training in Porur
    Big Data Analytics Training in Adyar

    ReplyDelete
  14. Thanks for this greatful information. all this information is very important to all the users and can be used good at all this process
    Best Tally ERP 9.0 Training Academy in Kanchipuram

    ReplyDelete
  15. Your article was so impressive and informative. Its very interesting to read. Thanks for sharing,
    Best web design Training Institute in Kanchipuram

    ReplyDelete
  16. Excellent post. I have read your blog it's very interesting and informative. Keep sharing.
    Best JAVA / J2EE / J2ME Course Training Institute in kanchipuram|

    ReplyDelete
  17. Nice post.. Really you are done a wonderful job. Thanks for sharing such wonderful information with us. Please keep on updating...
    Best C & C++ Course Training Institute in kanchipuram|

    ReplyDelete

 
Top