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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:45:58+00:00 2026-06-15T15:45:58+00:00

keySet() returns java.util.Set. Why doesn’t cast to NavigableSet throw a ClassCastException? This can be

  • 0

keySet() returns java.util.Set.
Why doesn’t cast to NavigableSet throw a ClassCastException?
This can be if real Object is a TreeSet with the java.util.Set reference. I can`t understand that.

import java.util.*;
class A1{}
public class Main{
     public static void main(String args[]){
         SortedMap nvs=new TreeMap();
         nvs.put(1,"one");
         nvs.put(2,"two");
         nvs.put(3,"three");
         nvs.put(4,"four");
         NavigableSet nss=(NavigableSet)nvs.keySet();
         for(Object ob: nss){
              System.out.print(nvs.get(ob)+", ");
         }
     }
}
  • 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-15T15:45:59+00:00Added an answer on June 15, 2026 at 3:45 pm

    The best thing to do would be to look at actual code of TreeMap (this is from java 1.7):

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

    And here’s the KeySet class declaration:

    static final class KeySet<E> extends AbstractSet<E> implements NavigableSet<E> {...}
    

    As you can see, TreeMap#keySet method returns a type Set which is a super interface of NavigableSet. And the Set reference returned from it points to a KeySet instance, as in navigableKeySet() method.

    And since KeySet class implements NavigableSet, you can always cast a reference pointing to it’s instance to a NavigableSet reference.

    Just to make sure that you are independent of keySet() method implementation you could simply call navigableKeySet() method to get NavigableSet and avoid the cast

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

Sidebar

Related Questions

I get this exception when i try to cast an Object array to a
Is there a Java Map keySet() equivalent for C++'s std::map ? The Java keySet()
I have something like this: Map<String, String> myMap = ...; for(String key : myMap.keySet())
I'm working with a service which returns JSON which can be converted to Map
How can I improve performance of this chunk of code ? What would be
I have this class in Java (it's from JaCoCo Project): public class MemoryMultiReportOutput implements
Map<String,String> map=request.getParameterMap(); ^ is the unmodifiable map. Set s1= map.keySet(); Set s2= map2.keySet();/* another
I'm getting the following errors: Exception in thread main java.util.ConcurrentModificationException at java.util.HashMap$HashIterator.nextEntry(HashMap.java:810) at java.util.HashMap$KeyIterator.next(HashMap.java:845)
This customized Valuecomparator sorts the TreeMap by it's value. But it doesn't tolerate nullpointexception
I have a HashMap<String,String> and there is a static method which returns this map

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.