Couple of things that if you knew you’d probably be able to solve your problem…

 

  1. When using “Hash” type collection you must override hashCode and equals and _not_ compareTo. Compare would work if you were using tree based collections. In other words, override equals and hashCode and make it work against the name field of the object.
  2. I’m not sure what you mean by “prop1 == prop2”. Whenever you compare objects like this it compares the references (the value of the references of the objects). This will not compare the objects by the values in the objects. The fact that your “prop1 == prop2” is merely a coincidence, you’re just lucky.

 


From: Fritz Meissner [mailto:[EMAIL PROTECTED]
Sent: 10 May 2006 05:47 PM
To: [email protected]
Subject: [CTJUG Forum] Help with comparing generics

 

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
-~----------~----~----~----~------~----~------~--~---

Reply via email to