File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Generics : defining their relationship in order to ovloard a method Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Generics : defining their relationship in order to ovloard a method" Watch "Generics : defining their relationship in order to ovloard a method" New topic
Author

Generics : defining their relationship in order to ovloard a method

James JVogt
Greenhorn

Joined: May 21, 2007
Posts: 6
I want to do something like:



but with generics:



of course the compiler complains that both puts have the same erasure and thus signature. Is something like this possible in Java? I would rather not make two methods "putT1(...)" "putT2(...)" since that would confuse things. Overloading by return type would also be great (for get() methods)... but I am pretty sure that is impossible in Java.
(I need this "double map" to ensure that the maps vary together in a specific way when one or the other is changed).
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16681
    
  19

Well, you could try it as a generic method...



This way, you can support both types without using overloading... unfortunately, the issue here is that you can't limit it to only two types.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
James JVogt
Greenhorn

Joined: May 21, 2007
Posts: 6
That actually won't work either since you can't determine what type you got (at runtime) and then put that type the appropriate map. I.e., I can't do



but I found that my request is impossible in Java due to generics being implemented via erasure.

Well I suppose I could have the user pass in two class objects to the constructor... and compare the classes of the passed in objects against those class objects.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16681
    
  19

That actually won't work either since you can't determine what type you got (at runtime) and then put that type the appropriate map. I.e., I can't do


Hmmm.... you're absolutely correct. Without the instanceof operator, it isn't possible to determine the type. I figured out a way of avoiding the need for the operator -- by using maps of maps.... but even in that case, I couldn't avoid a typecast.

And regardless, this doesn't solve your problem of restricting it to only two types.

Anyway, in case you are interested.... here is my (somewhat broken) solution...



Henry
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Henry Wong wrote:

That should be

This still allows you to put HashMap instances, but should you want to change you can turn it into a TreeSet or LinkedHashMap by only changing line 9 of your original code.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Generics : defining their relationship in order to ovloard a method
 
Similar Threads
Generics
Generics
Generics
Generics Methods
confused with generics