aspose file tools*
The moose likes Java in General and the fly likes I want to Use Generic with Wild Card Able to get Mulitple Object specialy two types, Declare?, Use? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "I want to Use Generic with Wild Card Able to get Mulitple Object specialy two types, Declare?, Use?" Watch "I want to Use Generic with Wild Card Able to get Mulitple Object specialy two types, Declare?, Use?" New topic
Author

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

PankajKumar Mishra
Greenhorn

Joined: Nov 09, 2010
Posts: 10
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...

P K M The Tech
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4460
    
    8

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

Joined: Nov 09, 2010
Posts: 10
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

Joined: Apr 06, 2010
Posts: 4460
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I want to Use Generic with Wild Card Able to get Mulitple Object specialy two types, Declare?, Use?