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

The Archive Base Latest Questions

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

Map map = new HashMap(); System.out.println(hashCode:+map.hashCode()); //hashcode==0 why? map.put(test,test); System.out.println(hashCode:+map.hashCode()); //hashcode be okay here

  • 0
Map map = new HashMap();
System.out.println("hashCode:"+map.hashCode()); //hashcode==0 why?

map.put("test","test");
System.out.println("hashCode:"+map.hashCode()); //hashcode be okay here

How can I get hashCode after Map map = new HashMap(); ? (like: new Object().hashCode())

  • 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-05-23T14:58:31+00:00Added an answer on May 23, 2026 at 2:58 pm

    The hashCode for maps is defined as the sum of all hashcodes of the keys and values.

    More precisely, Map.hashCode() is specified as:

    Returns the hash code value for this map. The hash code of a map is defined to
    be the sum of the hash codes of each entry in the map’s entrySet() view.
    This ensures that m1.equals(m2) implies that m1.hashCode()==m2.hashCode() for any
    two maps m1 and m2, as required by the general contract of Object.hashCode().

    And Map.Entry.hashCode() is defined as

    Returns the hash code value for this map entry. The hash code of a map entry e is defined to be:

    (e.getKey()==null   ? 0 : e.getKey().hashCode()) ^
    (e.getValue()==null ? 0 : e.getValue().hashCode())
    

    This ensures that e1.equals(e2) implies that e1.hashCode()==e2.hashCode() for any two
    Entries e1 and e2, as required by the general contract of Object.hashCode.

    Your new map has no entries yet, thus the sum is 0. Everything is at is should be.

    If you want a number like the unoverridden Object.hashCode() would return, use System.identityHashCode(map). This is a (more or less unique) number for each map, which will not change when the contents of the map changes.

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

Sidebar

Related Questions

code: Map<Integer,DealCountUpdater> dealCountMap=new HashMap<Integer,DealCountUpdater>(); public void update(){ for(Map.Entry<Integer, DealCountUpdater> e:new HashMap<Integer,DealCountUpdater>(dealCountMap).entrySet()){//line:58 System.out.println(e.hashCode()); } }
why do i get this exception? Map myHash = null myHash = (HashMap)Collections.synchronizedMap(new HashMap());
Gson gson = new Gson(); Map<String,Object> map = new HashMap<String, Object>(); map.put(a, 1); map.put(b,
Did you know that : Map<Object,Object> m1 = new HashMap<Object, Object>(); Map<Object,Object> m2 =
I have a nested map: Map<Integer, Map<Integer, Double>> areaPrices = new HashMap<Integer, Map<Integer, Double>>();
I have the following code private Map<KEY, Object> values = new HashMap<KEY, Object>(); public
I have the following code: Map<String, ObjectType> objectMap = new HashMap<String, ObjectType>(); for (ObjectType
I have a HashMap with various keys and values, how can I get one
If i use: HashMap<String, Integer> test = new HashMap<String, Integer>(); Or i use: HashMap
Can someone explain why the HashMap acts like it does in this example: Simple

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.