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 ;-).
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
- [CTJUG Forum] Help with comparing generics Fritz Meissner
- [CTJUG Forum] Re: Help with comparing generics Renier Rhode
- [CTJUG Forum] Re: Help with comparing generics klutz
- [CTJUG Forum] Re: Help with comparing generics Fritz Meissner
- [CTJUG Forum] Re: Help with comparing generics klutz
- [CTJUG Forum] Re: Help with comparing generics Fritz Meissner
- [CTJUG Forum] Re: Help with comparing gene... klutz
- [CTJUG Forum] Re: Help with comparing... Fritz Meissner
- [CTJUG Forum] Re: Help with compa... Fritz Meissner
- [CTJUG Forum] Re: Help with c... Jeff Mutonho
- [CTJUG Forum] String.replaceA... Java Mad
