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

  • Home
  • SEARCH
  • 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 617495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:26:31+00:00 2026-05-13T18:26:31+00:00

Here is what I would like to write in my java code: private <A

  • 0

Here is what I would like to write in my java code:

private <A extends Action<R>, R extends Result> MyType<A,R> member;

This is invalid syntax however. So I end up writing:

private MyType<? extends Action<? extends Result>, ? extends Result> member;

But this disregard the fact that both classes derived from Result are the same. My class methods all enforce this relationship, so I can be sure that MyType enforces it, but I still have to unsafely typecast member in some instances.

More details

Here is the precise version of what I want to do, although it is much more criptic:

I wish I could do:

private <A extends Action<R>, R extends Result> 
    Map< Class<A>, ActionHandler<A,R> > handlers;

Instead I have to do:

private Map< Class< ? extends Action<? extends Result> >, 
             ActionHandler<? extends Action<? extends Result>, 
                           ? extends Result> > handlers;

My methods enforce the desired relationship and look like that:

public <A extends Action<R>, R extends Result> void addHandler( 
    ActionHandler<A, R> handler ) {
  handlers.put( handler.getActionType(), handler );
}

I would like the following method:

public <A extends Action<R>, R extends Result> ActionHandler<A, R> 
    findHandler( A action ) {
  return handlers.get( action.getClass() );
}

But this doesn’t work: I have to add a cast and a @SuppressWarnings("unchecked").

Things I tried

I tried creating a new class just for that purpose :

public class MyMap <A extends Action<R>, R extends Result> extends 
    HashMap< Class<A>, ActionHandler<A,R> > { ... }

MyMap< ?, ? > handlers;

But it didn’t work, I still need a cast.

  • 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-13T18:26:31+00:00Added an answer on May 13, 2026 at 6:26 pm

    There’s no clean way to do this. The best you can do is what you’ve already done — hide the Map behind methods that enforce type safety, and hide the necessary casts in those methods.

    From Effective Java 2nd edition, Item 29: “Consider typesafe heterogeneous containers” (in which Josh Bloch sketches out a simpler version of what you’re doing, a “map” where the keys are classes and the values are instances of the key class):

    The next thing to notice is that the value type of the favorites Map is simply Object. In other words, the Map does not guarantee the type relationship between keys and values. In fact, Java’s type system is not powerful enough to express this. But we know that it’s true, and we take advantage of it when it comes time to retrieve a favorite.

    Given that you can’t get any useful type enforcement for the values anyway, the least messy implementation is probably something like this:

    public class HandlerRegistry
    {
        private Map<Class<?>, Object> map = new HashMap<Class<?>, Object>();
    
        public <R extends Result, A extends Action<R>>
            void addHandler(Class<A> actionClass, ActionHandler<R, A> handler) {
            map.put(actionClass, handler);
        }
    
        @SuppressWarnings("unchecked")
        public <R extends Result, A extends Action<R>> ActionHandler<R, A>
            findHandler(A action) {
                return (ActionHandler<R, A>) map.get(action.getClass());
        }
    }
    

    One thing to note about a Map-based solution: if action isn’t exactly the class used as the key, map.get() isn’t going to work — for instance, if it’s a subclass, or if the key class is an interface and the action itself is the implementation. You might be better off with:

        @SuppressWarnings("unchecked")
        public <R extends Result, A extends Action<R>> ActionHandler<R, ? super A>
            findHandler( A action ) {
                for ( Map.Entry<Class<?>, Object> entry : map.entrySet() )
                {
                    if (entry.getKey().isAssignableFrom(action.getClass())) {
                        return (ActionHandler<R, ? super A>) entry.getValue();
                    }
                }
                return null;
        }
    

    (Note: Originally the second example above returned ActionHandler<R, A>, which isn’t actually correct — it should be ? super A. (We don’t know what it can handle besides A — it could be any superclass of A, all the way up to Object.) In this particular case it’s probably safe enough, but if you consider something like a List, we could be in a lot of trouble: you can safely put an A into a List<? super A>, but if you assume you’re only going to get As out of it, you’re going to get ClassCastExceptions.)

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

Sidebar

Ask A Question

Stats

  • Questions 306k
  • Answers 306k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer g++ is gcc, it just automatically links to the standard… May 13, 2026 at 9:19 pm
  • Editorial Team
    Editorial Team added an answer This works in bash, according to this link: unzip \*.zip May 13, 2026 at 9:19 pm
  • Editorial Team
    Editorial Team added an answer You could put the function in your loop: for (var… May 13, 2026 at 9:19 pm

Related Questions

Please excuse my ignorance on the subject. I would like to write an application
I have a double-precision floating point field in my database that stores seconds past
I have a website in which I am migrating membership from ASP.NET services to
I'm trying to design a code where one guess a number. I defined the
I have a question regarding how to get the white- and black-listing feature of

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.