The normal idiom for Comparable in Generics is "T extends
Comparable<T>". So your Pair class would look like this:
public class Pair<T1 extends Comparable<T1>, T2 extends Comparable<T2>>
implements Comparable<Pair<T1 extends Comparable<T1>, T2 extends
Comparable<T2>>
... at least I think that's right. That's assuming that your
compareTo(Pair p1, Pair p2) is going to use the compareTo methods of T1
and T2 to do the comparison.
As for immutability and generics, the two concepts are orthogonal.
PC2
-----Original Message-----
From: java400-l-bounces@xxxxxxxxxxxx
[mailto:java400-l-bounces@xxxxxxxxxxxx] On Behalf Of James Perkins
Sent: March 25, 2009 09:05
To: Java Programming on and around the iSeries / AS400
Subject: Comparable and Generics
Hello All,
I'm wondering if anybody has experience implementing Comparable<T> over
a generic class. I know I could make sure the generic type implements
Comparable, but is that the only way?
Basically, I'm just creating a generic Pair class and would like to
implement Comparable, or Comparator, so that the Pair could be
successfully used in a TreeMap or any other sortable collection.
I could do this:
public class Pair<T1 extends Comparable, T2 extends Comparable>
implements Comparable<T1, T2>...
But would prefer this:
public class Pair<T1, T2> implements Comparable<T1, T2>...
If the full source would be helpful, just let me know.
Also, is there a way to make a truly immutable generic class? Something
like adding a mutable object like Date would be immutable in the Pair
object.
Normally I would just return a new Date(), but is there way to make it
immutable in another object?
Thanks in advance,
--
James R. Perkins
--
This is the Java Programming on and around the iSeries / AS400
(JAVA400-L) mailing list To post a message email: JAVA400-L@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit:
http://lists.midrange.com/mailman/listinfo/java400-l
or email: JAVA400-L-request@xxxxxxxxxxxx Before posting, please take a
moment to review the archives at
http://archive.midrange.com/java400-l.
As an Amazon Associate we earn from qualifying purchases.