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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:01:17+00:00 2026-05-17T18:01:17+00:00

I’m referring to the paradigm in Item 34 in Effective Java by Joshua Bloch.

  • 0

I’m referring to the paradigm in Item 34 in Effective Java by Joshua Bloch.  I would like to take the method he’s using which is to have each related enum implement a base interface, and initialize an EnumMap from the “sub-enums.” See the code section below.  I’m getting a syntax error which I don’t understand.  I’m not set on this method of implementation, but I would like to understand why it won’t work.

Note that this example assumes each class definition is in its own file.

public interface BaseEnum { 
    ... 
}

public enum EnumOps1 implements BaseEnum { 
    ... 
}

public class Widget {
    public Widget() {
         regMap = new EnumMap<EnumOps1, WidgetData>(EnumOps1.class);

         for (EnumOps1 op : EnumOps1.values()) {
             regMap.put(op, getWidgetData(op.key()));  // line with syntax error
         }
    }

    protected Map<? extends BaseEnum, WidgetData> regMap;
} 

Syntax error detail:

method put in interface java.util.Map<K,V> cannot be applied to given types
required: capture#1 of ? extends BaseEnum, WidgetData
found: EnumOps1, WidgetData

  • 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-17T18:01:18+00:00Added an answer on May 17, 2026 at 6:01 pm

    It’s a problem with your wildcarding. You should declare your map as Map<BaseEnum, WidgetData> and also your EnumMap as ex. HashMap<BaseEnum, WidgetData>

    There’s plenty of discussion of why this is true on SO, but see What is PECS (Producer Extends Consumer Super)? for example.

    Edit

    Sadly, you’re right – you can’t use an EnumMap there. This is because you are trying to use an interface, and EnumMap stipulates (as it requires a type T extends Enum<T>) that it must be an Enum only.

    Your choices basically boil down to

    1) Use an EnumMap<EnumOps1,...> and lose out on the polymorphism

    2) Use a HashMap<BaseEnum,...> and everything works fine but you have to use a non-Enum map.

    3) Use wildcards as you are trying, but you’ll run into the PECS restrictions I linked earlier e.g. you can add or remove elements but not both (super vs extends)

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

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.