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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:56:14+00:00 2026-06-07T15:56:14+00:00

I have a Map whose keys are of generic type Key<T> , and values

  • 0

I have a Map whose keys are of generic type Key<T>, and values are of type List<T>. If the key is an instance of Key<String>, the value must be a List<String>, and the same rule applies to any other key-value pairs. I have tried the following but it does not compile:

Map<T, List<T>> map;

At present I have to declare it with “partial” generics:

Map<Object, List> map;

I know this is bad but I currently have no better choice. Is it possible to use generics in this situation?

UPDATE

Maybe I didn’t express my problem clearly. I want a map that is able to:

map.put(new Key<String>(), new ArrayList<String>());
map.put(new Key<Integer>(), new ArrayList<Integer>());

And the following code should not compile:

map.put(new Key<String>(), new ArrayList<Integer>());

The key and value should always have the same generic type while the generic type can be any, and obviously extending a map does not meet my requirement.

  • 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-07T15:56:16+00:00Added an answer on June 7, 2026 at 3:56 pm

    I’m not aware of any existing library that does precisely this but it is not too hard to implement yourself. I’ve done something similar a few times in the past. You cannot use the standard Map interface but you can use a hash map inside to implement your class. To start, it might look something like this:

    public class KeyMap {
      public static class Key<T> { }
    
      private final HashMap<Object,List<?>> values = new HashMap<Object,List<?>>();
    
      public <T> void put(Key<T> k, List<T> v) {
        values.put(k, v);
      }
    
      public <T> List<T> get(Key<T> k) {
        return (List<T>)values.get(k);
      }
    
      public static void main(String[] args) {
        KeyMap a = new KeyMap();
        a.put(new Key<String>(), new ArrayList<String>());
        a.get(new Key<Integer>());
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two maps whose keys are String s and whose values are Set<MyObject>
I have a Map whose key and value and custom classes. The key class
I have a map declared as follows: map < string , list < string
I have a Map that uses a Set for the key type, like this:
I have a list of strings whose values come from a fixed set. I
I'm trying to create a map whose key and value are both structs (stE
I have an array whose indices map back to Enum values. I would like
Let's say I have a list a in Python whose entries conveniently map to
I have declared the following method: private void mockInvokeDBHandler(Map<String, Object>... rows) { List<Map<String, Object>>
Give I have a 3D map of type whose the length and width are

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.