You need to override the equals method on Proposition : ( Collections
use the equals method to check for equality )

You have to take care when you override the equals method that you
implement the contract of the equals method properly... ( Check the
JavaDoc on java.lang.Object )...

If you don't... you get some VERY funky behaviour on collections... :)

.... this SHOULD implement the contract of equals() correctly... just
give it another pair of eyballs...

public boolean equals( Object o ) {
    if ( ! o instanceof Proposition ) {
      return false;
    }
    if ( o == null ) {
      return false;
    }
    return ObjectUtils.equals(getName(),other.getName());
}

ObjectUtils is part of commons-lang and takes care of all the
additional null checking...

The lazy developers friend.. :):):)

Cheers,
Renier

On 5/10/06, Fritz Meissner <[EMAIL PROTECTED]> wrote:
> Hi all,
>
> Sorry for the verbosity of the post. I have been looking all over the net to
> find the solution to this one. I'm sure that this must be obvious if you
> know a lot (or some) about generics. I don't. :/
>
> I have written a class called Proposition, which has a String field called
> Name. I have two HashSet<Proposition> collections, and I want to perform set
> operations on them - specifically, I am calling HashSet.retainAll() to
> perform an intersect-like operation on the two sets. I want the Proposition
> objects to be compared *by their Name field only*, so I wrote the
> Proposition class to implement Comparable<Proposition>, and overwrote the
> compareTo() method in the interface. I know that this works, because if do I
> this:
>
> Proposition prop1 = new Proposition("test");
> Proposition prop2 = new Proposition("test");
>
> and I call prop1==prop2 I get the right result  (I also made the compareTo()
> method give output so that I know I am calling this method). But now if I do
> THIS:
>
>          HashSet<Proposition> testSet1 = new HashSet<Proposition>();
>          HashSet<Proposition> testSet2 = new HashSet<Proposition>();
>
>          Proposition prop1 = new Proposition("test");
>          Proposition prop2 = new Proposition("goneElement");
>          Proposition prop3 = new Proposition("test");
>
>          testSet1.add(prop1);
>          testSet1.add(prop2);
>
>          testSet2.add(prop3);
>
>          testSet1.retainAll(testSet2);
>
>           System.out.println(testSet1.size());
>
> I get "0". It is clearly not seeing prop3 and prop1 as equal - but then this
> is not surprising, since the output I put in Proposition's compareTo()
> method is not being displayed. Presumably for some obscure reason Java is
> calling something else to do the comparison. Someone please tell me where,
> and what I do to make it behave the way I want it to ;-).
>
>
>  >
>


--
Regards,

Renier Rhode
mobile: 083 556 0804
e-mail: [EMAIL PROTECTED]

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CTJUG Forum" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/CTJUG-Forum
For the ctjug home page see http://www.ctjug.org.za
-~----------~----~----~----~------~----~------~--~---

Reply via email to