Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

I want to Use Generic with Wild Card Able to get Mulitple Object specialy two types, Declare?, Use?

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using ConcurrentHashMap for storing list of values to be fetched from XML file.

Ex.

ConcurrentHashMap<String, String> paramMap = new ConcurrentHashMap<String, String>();

In new Case I needs to add ArrayList and String in paramMap

i.e. I want something Like ---

ConcurrentHashMap<String, ?> paramMap = new ConcurrentHashMap<String, ?>(); /// Generating Compiler Error

ConcurrentHashMap<String, ?> paramMap = new ConcurrentHashMap();/// Ok

paramMap("key","Value"); //Compiler error not allowed

paramMap("key",arrayList); //Compiler error not allowed

Capable of accepting any type., and implicit conversion to respective data type. i.e. either in String or ArrayList


Please help me If something is possible like that...
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String and ArrayList don't have anything in common, so if you really want to have a map that can contain both (and I'd personally be looking for a different solution), just use Map<String, Object>. You're not gaining anything with wildcards.

The reason what you've got doesn't compile is that Map<String, ?> means "a map of some unknown but specific type". That's why you can't add anything to it - because the compiler doesn't know whether it's safe or not. It could be a Map<String, Integer>, for example.
 
PankajKumar Mishra
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Matthew Brown wrote:String and ArrayList don't have anything in common, so if you really want to have a map that can contain both (and I'd personally be looking for a different solution), just use Map<String, Object>. You're not gaining anything with wildcards.

The reason what you've got doesn't compile is that Map<String, ?> means "a map of some unknown but specific type". That's why you can't add anything to it - because the compiler doesn't know whether it's safe or not. It could be a Map<String, Integer>, for example.




Thanks Matthew for your valuable suggestion, all though the concept you are suggesting I have already implemented, If I am doing so then there is no need of using Generic with this Map.
because ultimately I have to type case for my specific Data type.

Thanks once again. But I would like to know your solution when you find it.

Thanks in advance...
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, my solution would be to uise a completely different approach, but since I don't know what you're trying to achieve then I can't tell what that approach would be. But putting two unrelated types into the same collection, unless you don't care what the types are and a just using them as objects, seems wrong to me. And generics can't help you.
 
    Bookmark Topic Watch Topic
  • New Topic