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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:30:27+00:00 2026-06-13T17:30:27+00:00

In java, when I use a String as a key for Hashmap I get

  • 0

In java, when I use a String as a key for Hashmap I get a little different result than when I use the string hashcode as a key in the HashMap.

Any insight?

  • 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-13T17:30:28+00:00Added an answer on June 13, 2026 at 5:30 pm

    when I use the string hashcode as a key in the HashMap.

    You mustn’t use the hash code itself as the key. Hash codes aren’t intended to be unique – it’s entirely permitted for two non-equal values to have the same hash code. You should use the string itself as a key. The map will then compare hash codes first (to narrow down the candidate matches quickly) and then compare with equals for genuine string equality.

    Of course, that’s assuming your code really is as your question makes it, e.g.

    HashMap<String, String> goodMap = new HashMap<String, String>();
    goodMap.put("foo", "bar");
    
    HashMap<Integer, String> badMap = new HashMap<Integer, String>();
    badMap.put("foo".hashCode(), "bar");
    

    If that’s really what your code looks like, just use HashMap<String, String> instead.

    From the docs for Object.hashCode() (emphasis mine):

    The general contract of hashCode is:

    • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
    • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
    • It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use HashMap<String, ArrayList<String>> in Java. When input value is comes, For example, input
I need a regex to use in Java to replace a String with UNKNOWN
I read that in Java, String is immutable, so we can't really use toLowerCase
Is it possible to use Java FutureTask with a Spring TaskExecutor to get a
Description: If we use java objects jruby get permgen too: System.out.println(Initialazing..); //Spring applicaton context
i use java reflections to get methods used in a class.I also want to
I have wanted to use a HashMap that maps a value of type String
I have a stemming algorithm in Java which requires a static final HashMap<String, String>
When doing internationalization in Java, you assign a string key to each message. What's
I'm just starting to learn to use HashMap and reading the java tutorial, but

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.