Sets are not necessarily unordered but they are unique.  Sets will have one and 
only one object of type <T>.  In order to achieve this, the object needs to 
show how it is unique from the others.  While the initial thought is that the 
"equals()" method would work, the specification for Set requires a compareTo 
method be present by implementing the Comparable interface.  You can also 
create a Comparator object that can be passed into the Set for comparisons.  
There is  good book from O'Reilly that talks about this.  It is called "Java 
Generics and Collections".  It goes into depth about the Sets and the various 
implementations of a Set.

Based on the original question, I have seen the ClassCastException many times.  
The remedy I have always used is to implement the Comparable interface.  I am 
not saying that the ClassCastException cannot be thrown for another reason.

-----Original Message-----
From: yoavsc [mailto:[email protected]]
Sent: Thursday, January 21, 2010 12:57 AM
To: [email protected]
Subject: RE: ClassCastException in pcReplaceField


Hello,
I'm afraid I misunderstand something.


Russell Collins wrote:
>
> Therefore the objects that are going into the Set collection need to
> implement the Comparable interface to be inserted.
>

Why is that so?
java.util.Set does not require the inserted type to implement Comparable.
Sets are unordered collections.
Checking the equality of two objects (using equals()) should be enough for
sets.

What am I missing?

Thanks,
Yoav
--
View this message in context: 
http://n2.nabble.com/ClassCastException-in-pcReplaceField-tp4392185p4432071.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

CONFIDENTIALITY NOTICE: The information contained in this electronic mail 
(email) transmission (including attachments), is intended by MCLANE ADVANCED 
TECHNOLOGIES for the use of the named individual or entity to which it is 
addressed and may contain information that is privileged, confidential and/or 
protected as a trade secret. It is not intended for transmission to, or receipt 
by, any individual or entity other than the named addressee(s). If you have 
received this email in error, please delete it (including attachments) and any 
copies thereof without printing, copying or forwarding it, and notify the 
sender of the error by email reply immediately.

Reply via email to