wood burning stoves 2.0
The moose likes Beginning Java and the fly likes Generic type MinMax Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Generic type MinMax" Watch "Generic type MinMax" New topic
Author

Generic type MinMax

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 662

I would like to create a generic type minmax, that generates the minmax depending on the type represented by <T>.



If you have better examples which help me understand the real use of generics in business, I would appreciate information, links, tutorials, and, source code.

Related links
1. Generics Tutorial (2004)
2. Java Generics FAQ, written by Angelika Langer (2010)


Jon
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 4087
    
  18

You can't, unless you know things about type T (for instance, whatever T is, the set of all unique T must be well-ordered and have an upper bound). So T must at least extend some interface that will provide this information. And if this interface provides this information, why would you need a generic class that does nothing but delegate to the methods defined in this interface?


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 20128
    
  30

Let's assume you want to do this as an exercise. I suggest you take a look at java.util.Collections and its min and max methods, and what their generic types are.
(Note: the "Object &" part can be omitted since every single type matches that.)


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic type MinMax
 
It's not a secret anymore!