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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:11:25+00:00 2026-06-11T08:11:25+00:00

If I define a Map<K,V> map = new HashMap<K, V>() why can’t I do

  • 0

If I define a Map<K,V> map = new HashMap<K, V>() why can’t I do something like:

map.put(new MyKey<CustomObject>(), new SomeOtherObject<CustomObject>());

But if I do it via another method it works i.e.:

add(new MyKey<CustomObject>(), new SomeOtherObject<CustomObject>());  

public <K, V> void add(K k, V v){  
        Map<K, V> threadLocalMap = new HashMap<K, V>();  
        threadLocalMap.put(k, v);  
}  
  • 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-11T08:11:27+00:00Added an answer on June 11, 2026 at 8:11 am

    You need to explicitly declare the types used in your map:

    Map<MyKey,SomeOtherObject> map = new HashMap<MyKey, SomeOtherObject>();
    map.put(new MyKey<CustomObject>(), new SomeOtherObject<CustomObject>());
    

    In your second example, the add method is generic, so when you call it, K and V are replaced by the actual type used in the calling code.

    EDIT

    You seem to misunderstand the meaning of Map<K, V>. K and V don’t have any specific meaning, they just show that you can “parameterize” the map to only accept a certain type of key and a certain type of value. But it is your responsibility to define those types.

    In your first example, you want to use MyKey as the type for the keys (the K) and SomeOtherObject for the values (the V). So you need to create a specific map for those types: Map<MyKey,SomeOtherObject> map = new HashMap<MyKey, SomeOtherObject>();.

    You can’t simply write Map<K, V> because K and V don’t mean anything to the compiler (unless you have create a class called K and a class called V, but I assume you haven’t).

    In your second example, you use a generic method which takes two parameters of type K and V. When the method is called, the K and the V are replace by the actual types received, so the code in your example is transformed to something very similar to the code at the top of my answer.

    I hope this clarifies things. I suggest that you spend time reading a tutorial on generics and look at concrete examples.

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

Sidebar

Related Questions

When we define routes in routes.rb using the name like map.some_link .We can use
If map is defined as private Map<Integer, Integer> map = new HashMap<Integer, Integer>(); Updating
I want to define a generic function for printing contents of std::map like types.
I would like to achieve something similar to how scala defines Map as both
I define a Map , and deposited inside objects, I would like to modify
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); Map<String, Object> returnMap = new HashMap<String, Object>(); for (int i
On REPL, if I define (def fits (map vector (take 10 (iterate inc 0))))
If you had two snippets: (global-set-key \C-d delete-char) and (define-key global-map \C-d delete-char) Is
I've got this snippet of code: (define-key lisp-interaction-mode-map (kbd C-c C-e) (lambda () (let
I am trying to define a mapping for Ctrl + . as follows: map

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.