Java: Is it possible to have a generic class that only takes types that can be compared?

Posted by mr popo on Stack Overflow See other posts from Stack Overflow or by mr popo
Published on 2010-05-05T23:59:32Z Indexed on 2010/05/06 0:08 UTC
Read the original article Hit count: 169

Filed under:
|

I wanted to do something along the lines of:

public class MyClass<T implements Comparable> {
    ....
}

But I can't, since, apparently, generics only accept restrictions with subclasses, and not interfaces.

It's important that I'm able to compare the types inside the class, so how should I go about doing this? Ideally I'd be able to keep the type safety of Generics and not have to convert the T's to Object as well, or just not write a lot of code overall. In other words, the simplest the better.

© Stack Overflow or respective owner

Related posts about java

Related posts about generics