Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9257331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:09:04+00:00 2026-06-18T12:09:04+00:00

Ok so SortedMap / SortedSet is an interface, and TreeMap / TreeSet is it’s

  • 0

Ok so SortedMap / SortedSet is an interface, and TreeMap / TreeSet is it’s implementation. Both of them keep the elements in sorted order, right? So why do we need TreeMap / TreeSet?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:09:05+00:00Added an answer on June 18, 2026 at 12:09 pm

    Interfaces do not provide any functionality, they just define the general outline of a class in terms of the methods it provides. But there’s no code inside SortedMap/SortedSet that implements how to actually achieve this functionality.

    And as a matter of fact, you can often have multiple ways to realize the same functionality. Think of the interface java.util.Set: you could implement it as a TreeSet but also as a HashSet. Usually, there are some trade-offs between different implementations: a hashset might provide faster access times on average, while a tree set may be better at keeping the order of its items.

    Often enough, however, a developer doesn’t really care about the implementation details, as long as they know that they can store items in a set and retrieve them. That basic idea, but not how to achieve it, is defined by the interface.

    This is also the reason you cannot instantiate an interface. If you try the following:

    SortedSet<Integer> set = new SortedSet<Integer>();
    

    your compiler will complain. That is because “SortedSet” does not really realize a set itself, it just defines what an implementation of a sorted set must provide in terms of methods.

    Here’s a contrived example. Imagine you want to offer a functionality to compute the percentage of positive integers in a set. You could define a method:

    public double getPercentageOfPositives(Set<Integer> set) {
        if (set.size() == 0) {
            return 0.0;
        }
    
        int count = 0;
    
        for (Iterator<Integer> iter = set.iterator(); iter.hasNext();) {
            if (iter.next() > 0) count++;
        }
    
        return 100.0 * count / set.size();
    }
    

    Here, you don’t really care whether the user of your method gives you a TreeSet or a HashSet. It doesn’t matter which principle the given class uses, because you’re just calling the size() method and the iterator() method anyway. All you need is trust in the fact that any set will have these two methods. An interface gives you that trust.

    Therefore your method signature only asks for a Set, which is an interface that defines that all classes implementing it must provide (amongst others) a size() and an iterator() method. If you wrote it like this:

    public double getPercentageOfPositives(SortedSet<Integer> set) {
        ...
    }
    

    and I have an instance of HashSet then I couldn’t use your method even though HashSet provides size() and iterator() as well. 🙁

    In that sense, an interface is like a super-class, it defines the commonalities that all classes must have that implement it. But it does not provide any functionality itself.

    Thus to come back to your original example of SortedSet: this interface does not provide any functionality. It merely defines which methods a sorted set implementation must provide. TreeSet is such an implementation.

    The same line of thought applies to SortedMap.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using a TreeMap (SortedMap) whose keys are Object[] with elements of varying types.
In Java there are the SortedSet and SortedMap interfaces. Both belong to the Java
I need to iterate through SortedMap's entry set (which is a SortedSet) backwards. The
Regarding Java TreeMap method (or NavigableMap interface): public SortedMap<K,V> headMap(K toKey) public NavigableMap<K,V> headMap(K
As far as I know, things such as SortedMap or SortedSet , use compareTo
Possible Duplicate: Java: SortedMap, TreeMap, Comparable? How to use? I am using the Java
Does there exist a cheat sheet for data structure like TreeMap, SortedMap, HashSet etc
I would like to convert a Map[Int, Any] to a SortedMap or a TreeMap
I would like to insert items into a HashMap, TreeMap or SortedMap (you may
I want to make a class usable in SortedSet | SortedMap . class MyClass

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.