• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generics Question

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am new to Java6.I have a basic question reagrding hashmap,arraylist,Set using Java6.

How should we define these when you are not sure what dataType they carry.

If i am sure that they carry String,I could define them as HashMap<String,String> abc = new HashMap<String,String>();

Can I do this,if I am not sure what dataType HashMap<?,?> abc = new HashMap<String,String>

From my reading "?" is used when you want to extend user defined class.(Correct me if I am wrong).

Can you also point me where I can get examples on how to use generics.

Thanks
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Start in the Java™ Tutorials. There is (I think) a second generics section in the Tutorials. Google for Angelika Langer Java Generics FAQ, where you should find lots of useful information.

I am not sure but I think <?, ?> will not work the way you expect. You should start off by declaring and instantiating your generic classes to the types you intend to use. The idea is to restrict your Collections etc., to one particular type.
 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You got to understand one thing.

Eventhough you define a variable as Genric reference with ? you cannot instantiate any collection type with ?.

You have to tell the compiler the type of generic you are about to use for this collection type like new ArrayList<string>() etc...

So if you are not sure what data type they carry you can declare the Generic type as Object because all the objects inthe Java are Object.

But it has some restriction too when you use this type as a parameter in a method.

If you declare the parameter as List<Object> then you can pass only List<Object> or ArrayList<Object>. Not any other Generic type.

Note: Polymorphism works only for base type (List or ArrayList) not for Generic type(in this case Object).

So in that case you can use the parameter as List<?> that brings you another restriction that you can not add any object inside that method with that reference.
reply
    Bookmark Topic Watch Topic
  • New Topic