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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T04:23:58+00:00 2026-05-11T04:23:58+00:00

This questions is prompted by strange HashMap.put() behaviour I think I understand why Map<K,V>.put

  • 0

This questions is prompted by strange HashMap.put() behaviour

I think I understand why Map<K,V>.put takes a K but Map<K,V>.get takes an Object, it seems not doing so will break too much existing code.

Now we get into a very error-prone scenario:

java.util.HashMap<Long, String> m = new java.util.HashMap<Long, String>(); m.put(5L,'Five'); // compiler barfs on m.put(5, 'Five') m.contains(5); // no complains from compiler, but returns false 

Couldn’t this have been solved by returning true if the Long value was withing int range and the values are equal?

  • 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. 2026-05-11T04:23:58+00:00Added an answer on May 11, 2026 at 4:23 am

    Here is the source from Long.java

    public boolean equals(Object obj) {     if (obj instanceof Long) {         return value == ((Long)obj).longValue();     }     return false; } 

    I.e. it needs to be a Long type to be equal. I think the key difference between:

    long l = 42L int i = 42; l == i 

    and your example above is that with primitives an implicit widening of the int value can occur, however with object types there are no rules for implicitly converting from Integer to a Long.

    Also check out Java Puzzlers, it has a lot of examples similar to this.

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

Sidebar

Related Questions

I believe this questions has been asked in some or the other way but
First of I'm very sorry but this questions is not so so specific. All
Eric Willis' response to this question: https://stackoverflow.com/questions/563041/receive-emails-in-a-net-service-c prompted me to start thinking of creating
I'm trying to think of a regular expression for this but not having any
There are several questions that are similar to this, but none address this specific
This questions is a follow up on an answer by Paul Alexander to the
This questions involves 2 different implementations of essentially the same code. First, using delegate
This questions relating to infowindow in the google maps API v3.. Currently I loop
The answers to this questions: Where are the Java 7 updates for OpenJDK? asserts
I am baffled with this questions. I want to believe that through some Javascript

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.