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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:32:56+00:00 2026-05-14T06:32:56+00:00

I was surprised by the fact that Map<?,?> is not a Collection<?> . I

  • 0

I was surprised by the fact that Map<?,?> is not a Collection<?>.

I thought it’d make a LOT of sense if it was declared as such:

public interface Map<K,V> extends Collection<Map.Entry<K,V>>

After all, a Map<K,V> is a collection of Map.Entry<K,V>, isn’t it?

So is there a good reason why it’s not implemented as such?


Thanks to Cletus for a most authoritative answer, but I’m still wondering why, if you can already view a Map<K,V> as Set<Map.Entries<K,V>> (via entrySet()), it doesn’t just extend that interface instead.

If a Map is a Collection, what are the elements? The only reasonable answer is “Key-value pairs”

Exactly, interface Map<K,V> extends Set<Map.Entry<K,V>> would be great!

but this provides a very limited (and not particularly useful) Map abstraction.

But if that’s the case then why is entrySet specified by the interface? It must be useful somehow (and I think it’s easy to argue for that position!).

You can’t ask what value a given key maps to, nor can you delete the entry for a given key without knowing what value it maps to.

I’m not saying that that’s all there is to it to Map! It can and should keep all the other methods (except entrySet, which is redundant now)!

  • 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-05-14T06:32:56+00:00Added an answer on May 14, 2026 at 6:32 am

    From the Java Collections API Design FAQ:

    Why doesn’t Map extend Collection?

    This was by design. We feel that
    mappings are not collections and
    collections are not mappings. Thus, it
    makes little sense for Map to extend
    the Collection interface (or vice
    versa).

    If a Map is a Collection, what are the
    elements? The only reasonable answer
    is "Key-value pairs", but this
    provides a very limited (and not
    particularly useful) Map abstraction.
    You can’t ask what value a given key
    maps to, nor can you delete the entry
    for a given key without knowing what
    value it maps to.

    Collection could be made to extend
    Map, but this raises the question:
    what are the keys? There’s no really
    satisfactory answer, and forcing one
    leads to an unnatural interface.

    Maps can be viewed as Collections (of
    keys, values, or pairs), and this fact
    is reflected in the three "Collection
    view operations" on Maps (keySet,
    entrySet, and values). While it is, in
    principle, possible to view a List as
    a Map mapping indices to elements,
    this has the nasty property that
    deleting an element from the List
    changes the Key associated with every
    element before the deleted element.
    That’s why we don’t have a map view
    operation on Lists.

    Update: I think the quote answers most of the questions. It’s worth stressing the part about a collection of entries not being a particularly useful abstraction. For example:

    Set<Map.Entry<String,String>>
    

    would allow:

    set.add(entry("hello", "world"));
    set.add(entry("hello", "world 2"));
    

    (assuming an entry() method that creates a Map.Entry instance)

    Maps require unique keys so this would violate this. Or if you impose unique keys on a Set of entries, it’s not really a Set in the general sense. It’s a Set with further restrictions.

    Arguably you could say the equals()/hashCode() relationship for Map.Entry was purely on the key but even that has issues. More importantly, does it really add any value? You may find this abstraction breaks down once you start looking at the corner cases.

    It’s worth noting that the HashSet is actually implemented as a HashMap, not the other way around. This is purely an implementation detail but is interesting nonetheless.

    The main reason for entrySet() to exist is to simplify traversal so you don’t have to traverse the keys and then do a lookup of the key. Don’t take it as prima facie evidence that a Map should be a Set of entries (imho).

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

Sidebar

Related Questions

I was surprised recently to find that it's possible to have a return statement
I'm always surprised that even after using C# for all this time now, I
I'm new to ReSharper and am surprised that there isn't a template defined for
I just worked through a book on WCF and was surprised that it didn't
It's my understanding that StackOverflow (SO) was built using ASP.NET. What surprised me is
I'm familiar with the fact that you can create LaTeX reports directly from R
I was pleasantly surprised a few months ago to learn that I could use
I am surprised that the following calls result in type-cast warning: I have a
I was surprised to see that the following doesn't work as expected. define('CONST_TEST','Some string');
I was surprised to to learn that a class variable of a subclass can't

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.