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 8931035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:01:12+00:00 2026-06-15T09:01:12+00:00

I was reading about Collections, when this question stuck me. Following is the code

  • 0

I was reading about Collections, when this question stuck me.

Following is the code I wrote to test my doubt.

public static void main(String[] args) {        
    TreeMap<Integer, String> tree = new TreeMap<Integer, String>();
    tree.put(1, "1");
    tree.put(2, "2");
    Set<Integer> set = tree.keySet();
    System.out.println(set instanceof Set);
    System.out.println(set instanceof HashSet);
}

Result :

true
false

Above code says that my set object is a instance of Set. but Set is an Interface how can it be instantiated. I’m confused. 🙁

  • 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-15T09:01:13+00:00Added an answer on June 15, 2026 at 9:01 am

    Set is an interface, so no, you cannot directly instantiate it. Interfaces would be pretty useless if you couldn’t have an instance of one, though! The instance returned by tree.keySet() is some concrete implementation of the Set interface.

    Let’s get super-specific, and look at the TreeMap#keySet() source code:

    public Set<K> keySet() {
        return navigableKeySet();
    }
    

    Okay, that doesn’t tell us much. We need to drill down:

    public NavigableSet<K> navigableKeySet() {
        KeySet<K> nks = navigableKeySet;
        return (nks != null) ? nks : (navigableKeySet = new KeySet(this));
    }
    

    So the concrete type returned is a KeySet! There’s your implementation of the Set interface. http://www.docjar.com/html/api/java/util/TreeMap.java.html#1021

    Which explains this:

    System.out.println(set instanceof Set); // prints true
    System.out.println(set instanceof HashSet); // prints false
    

    Set is an interface; HashSet is an implementation of that interface. foo instanceof Set will be true for every instance foo of any Set implementation. We already established that the concrete type of the object returned by TreeMap#keySet() is a KeySet, not a HashSet, so that explains why set instanceof HashSet is false – because set is a KeySet, so it cannot be a HashSet!

    If that still doesn’t make sense to you, read up on instanceof:

    The instanceof operator compares an object to a specified type. You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.

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

Sidebar

Related Questions

I'm reading Expert F# book and I found this code open System.Collections.Generic let divideIntoEquivalenceClasses
Reading about Kohana templates and saw something I've never seen before: $this->template->title = __('Welcome
Reading about Django, I saw this: http://docs.djangoproject.com/en/1.1/ref/contrib/admin/#ref-contrib-admin - the fancy simple to use admin
I am reading the book MongoDB in Action . I have a question about
I was reading this book. Explaing about @OneToOne unidirectional, the author has taken the
I'm reading about SQLAlchemy's connection pooling, which has a default of 5 connections and
Reading about the Dispose pattern , I see the documentation repeatedly refer to cleaning
Reading about Delphi's Exit statement (see here for instance), I cannot ignore that writing
While reading about exception, I will always come across checked exceptions and unchecked exceptions,
When reading about pipes in Advanced Programming in the UNIX Environment, I noticed that

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.