aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes TreeSet & TreeMap confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "TreeSet & TreeMap confusion" Watch "TreeSet & TreeMap confusion" New topic
Author

TreeSet & TreeMap confusion

Ben Pheonix
Ranch Hand

Joined: Dec 11, 2012
Posts: 46
    
    1

Hello,
I am preparing for OCPJP7 and for that first sitting for OCPJP6 exam. I want to know in the javadocs, it is written that TreeSet is implemented using TreeMap
instance. What does it mean?
Thanks,
Ben


Twitter:ben_pheonix
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Welcome to CodeRanch

from source code of java.util.TreeSet :

So, TreeSet HAS-A TreeMap data structure(Red-Black tree) to store their objects
Ben Pheonix
Ranch Hand

Joined: Dec 11, 2012
Posts: 46
    
    1

Dear Seetha,

Thanks a ton for your prompt response. The confusion is, it says the TreeSet implementation is based on TreeMap. How is that?
I can see that TreeSet is composed of TreeMap. It means that, whatever the added functionality TreeMap provides like navigation
and searching is implicitly done in TreeSet via TreeMap. Am i am understanding it correct?

Thanks,
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 608
TreeSet data structure HAS-A TreeMap. In TreeMap, there is a key-value pair.
In TreeSet, there is a key-value pair as well. But in TreeSet, the value is always null. In TreeMap, the value is whatever value you declare.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Himai Minh wrote:But in TreeSet, the value is always null.

One minor correction(nit picking)
in TreeSet , Value is not null but almost like null(you cant access this in your code) as in
Don Redd
Ranch Hand

Joined: Jan 05, 2012
Posts: 82

it is written that TreeSet is implemented using TreeMap
instance. What does it mean?


its simple

as per definition Set does not allow duplicates
and its implemented using Map as map doesn't allow duplicate keys.

here elements of set are same as keys of map inside set.

as similar to TreeSet and TreeMap combination you can check Hashset and HashMap combination

Regards,
Don..Red
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: TreeSet & TreeMap confusion
 
Similar Threads
return type of headSet and headMap methods.
Comparable and comparator interface
Hashtable Problem!!!
TreeSet adding twice null
comparator and comparable interfaces