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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:12:54+00:00 2026-06-17T16:12:54+00:00

Is it possible to declare a Map that maps from keys of a specific

  • 0

Is it possible to declare a Map that maps from keys of a specific subclass to values of a specific subclass but ensuring that both classes share the same Type parameter?

For the background:

both ClassA and ClassB implement behaviour for a common resource

public abstract  class ClassA<T> {
      public abstract T getResource() ;
}

public abstract class classB<T> {
       public  abstract void consoumeResource(T resource);
}

i want to map from implementations of ClassA and ClassB and ensure only “compatible” pairs can be put together in one entry.

  • 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-17T16:12:55+00:00Added an answer on June 17, 2026 at 4:12 pm

    Another way would be to provide your own Map implementation. There’s not much code needed if you extend an existing implementation, and use your new type:

    public class CompatibleHashMap<T> extends HashMap<ClassA<T>, ClassB<T>> {
    
    }
    

    Now, a CompatibleHashMap<String> only lets you put ClassA<String> as keys and ClassB<String> as values.

    EDIT:

    As you mentioned in your comment, this way you are tying yourself to a Map implementation. You can overcome this by doing something like the following:

    public class CompatibleMap<T> implements Map<ClassA<T>, ClassB<T>> {
    
        private Map<ClassA<T>, ClassB<T>> map;
    
        public CompatibleMap(Map<ClassA<T>, ClassB<T>> map) {
            this.map = map;
        }
    
        @Override
        public Set<List<T>> keySet() {
            return map.keySet();
        }
        // ... implement all other Map methods by calling the method on map.
    }
    

    You can then instantiate it like

    CompatibleMap<String> map = new CompatibleMap<>(new HashMap<ClassA<String>, ClassB<String>>());
    

    This way, you are not tied to a specific Map implementation, and the compiler will throw an error if the generic types of the map, ClassA and ClassB are not the same.

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

Sidebar

Related Questions

It is possible? DECLARE @vTableName varchar(50) SET @vTableName = (SELECT TableName FROM qms_Types WHERE
I'm trying to export classes from a DLL that contain objects such as std::vectors
Is it possible to declare a style that sets the fontsize of tooltips to
Is it possible to declare a class in C# that is only valid to
I have a Map whose keys are of generic type Key<T> , and values
Is it possible to declare a composite-component in such a way that it can
is it possible to declare constants in /application/config/constant.php from a DB table? I'm trying
Is it possible to declare that a column in MySQL should be unmodifiable once
Possible Duplicate: Is it possible to declare an array as constant Is it possible
Is it possible to declare more than one variable using a with statement in

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.