× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



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.

This thread ...

Follow-Ups:
Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.