• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

How to define the type of an Collection at run time

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how to define the type of a collection at runtime.

For example,

if i have an Arraylist which has to be created based on the type, may be integer sometimes, or String or any object. I will know the type only at runtime.

I cannot declare the arraylist like ArrayList<String> arrList = new ArrayList<String>(), as this will not work for any other object type. I do not want to declare the arraylist raw as well.

Please suggest......
 
Sheriff
Posts: 28346
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't specify the generic type at run time.
 
Sheriff
Posts: 22818
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct. Search for "type erasure" to find out why.
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
not exactly "run time", but this can help you:



My knowledge about generics is bad, someone please correct me if wrong...
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
means anything and everything is excepted by the list.....

You wouldn't like to do this your type safety is gone for a toss unless you have very good reason to do so
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prabhat Jha wrote: means anything and everything is excepted by the list.....

You wouldn't like to do this your type safety is gone for a toss unless you have very good reason to do so



ye, i agree with that. I do not know a situation where i do not know what my collection will hold.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot specify the type of a collection at runtime, and it is not useful to do so.

Java is a statically typed language. That means that the compiler needs to know all the types of the variables when you compile the program. If you don't know the type at compile time, you can't make use of Java's static type checking. If you know the type only at runtime, then that's too late for type checking by the compiler - because when your program is running, it has already been compiled.

So, there is no way to do this. It's like you want to eat the cake before you bake it.
 
Rob Spoor
Sheriff
Posts: 22818
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prabhat Jha wrote: means anything and everything is excepted by the list.....


Actually, no. It means it can return anything and everything but you cannot add anything at all to the list. After all, the ? could in reality be String, Integer, Foo, ... The compiler can never know that what you add is allowed so it simply disallows everything.
 
It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic