Get all updates of Java4s
Follow Us: Email Newsletter

How to Store Multiple Data types In An ArrayList

Core Java » On Feb 21, 2014 { 6 Comments } By Sivateja

So, how to store objects of multiple data types in the ArrayList, in fact storing is pretty simple, every one will get some idea but how to retrieve the values ? for example if we have 100+ values in the ArrayList object of different types then ? let us see how to do handle that situation.

Example : ArrayList with Multiple Data Types


Value is Java4s
Value is 12
Value is 12.54


In the above application, we can print the values by converting our ArrayList object into the Array [ al.toArray() ] also, but in the real time, there may be a chance to add some user defined class objects into the ArrayList, in that scenario obviously the better approach will be converting to Object type and then check the type caste and go ahead.

You Might Also Like

  ♦ About Author ♦

Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.


6 Responses to “How to Store Multiple Data types In An ArrayList”
  1. machoka says:

    How about if you add an Arraylist of String in al. How do you print it out?

  2. C says:

    Why a raw type and not just use ArrayList ?! Why all the instanceof, when they all must be Objects and all have toString()? I don’t see what there is to learn from this “how to”.

  3. GIri says:

    Hi, awesome website.

    if we store null in the above arraylist object then we get exception in above program… Am I correct

  4. Venkat says:

    Hi All,

    It depends on what type of data you store in ArrayList. So accordingly we have to check all object types in ‘IF’ condition before printing.

    The above program is just an example only, this can be customized.

  5. Technosnoop says:

    Well explained article.
    I was searching for the solution and here i found one.
    Thanks once again.

  6. sidhartharaj says:

    Good Explanation….can i know that how to use foreach loop for above problem.


By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2017 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.