File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes TreeSet contains method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "TreeSet contains method" Watch "TreeSet contains method" New topic
Author

TreeSet contains method

Matthew Sessoms
Greenhorn

Joined: Apr 28, 2011
Posts: 6
Hello,
I've been trying a few things with sets and comparators etc.


I also have an appropriate compareTo() method. I can't figure out why the first print statement prints false. However, if I were not to add, say a[2] to the set, then the first line & the second line prints true.

Why is this happening?

Thanks!
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

Welcome to JR Matthew

Can you post your Comparator implementation and the contents of the AggTuples class ?


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

PS: Class names usually start with a capital letter. That is the standard. practice -> Practice
Matthew Sessoms
Greenhorn

Joined: Apr 28, 2011
Posts: 6
Sure, thanks for the reply.
Dragos Nica
Ranch Hand

Joined: Oct 25, 2009
Posts: 39
Matthew Sessoms wrote:Sure, thanks for the reply.



Your implementation for compareTo is not complete. You checked only if two objects of type AggTuples are equals or not, but the implementation of compare To should be able to provide addtional info as which object should be inserted before another, because lements inside a treeSet should be sorted.

This is a possible implementation of compareTo:


SCJP 6.0 (88%)
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

Comparators cannot be used to check equality. Implement the equals and hashCode contract.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: TreeSet contains method