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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:54:52+00:00 2026-05-22T02:54:52+00:00

The following code compiles but if I uncomment the commented line, it does not

  • 0

The following code compiles but if I uncomment the commented line, it does not and I am confused why. HashMap does extend AbstractMap and the first line where map is declared compiles fine.

import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Map;

public class Test {

    public static void main(String args[]) {
        Map<String, ? extends AbstractMap<String, String>> map = new HashMap<String, HashMap<String, String>>();
        //map.put("one", new HashMap<String, String>());
    }
}

And, I know the “right way” is this:

import java.util.HashMap;
import java.util.Map;

public class Test {

    public static void main(String args[]) {
        Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>();
        map.put("one", new HashMap<String, String>());
    }
}
  • 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-22T02:54:53+00:00Added an answer on May 22, 2026 at 2:54 am

    The first code is unsafe – imagine you’re actually written:

    HashMap<String, ConcurrentHashMap<String, String>> strongMap = 
        new HashMap<String, ConcurrentHashMap<String, String>>();
    Map<String, ? extends AbstractMap<String, String>> map = strongMap;
    

    Now:

    map.put("one", new HashMap<String, String>());
    ConcurrentHashMap<String, String> x = strongMap.get("one");
    

    We should have a ConcurrentHashMap – but in reality we’ve only got a HashMap.

    This is actually a lot simpler to explain if we reduce the amount of generics going on… your scenario is really equivalent to (say):

    List<? extends Fruit> list = new List<Apple>();
    list.add(new Apple());
    

    which looks okay, until you consider that it’s equivalent in validity (as far as the compiler is concerned) to:

    List<Apple> apples = new ArrayList<Apple>();
    List<? extends Fruit> list = apples;
    list.add(new Orange());
    Apple apple = list.get(0); // Should be okay... but element 0 is an Orange!
    

    which is obviously not okay. The compiler has to treat the two in the same way, so it makes both of them invalid.

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

Sidebar

Related Questions

The following code compiles fine, but does not allow the user to choose whether
The following code compiles in MSVC++, but does not compile in GCC 4.5.1: #include
The following code compiles with gcc 4.5.1 but not with VS2010 SP1: #include <iostream>
The following code compiles with G++ 4.6.1, but not with Visual Studio 2008 return
The following code compiles and runs but I'm not sure what exactly is going
The following code compiles, but when the method is called I get System.NotImplementedException: Not
The following code compiles correctly under VC++ 8 on XPSP3, but running it causes
The following code compiles in Visual C++ and gcc, but fails with Code Warrior
I have the following code, which compiles but doesn't bring back any data. Here
The following code doesn't compile with gcc, but does with Visual Studio: template <typename

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.