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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:25:04+00:00 2026-06-07T04:25:04+00:00

I am trying to understand what issues we can face if we implement hashCode()

  • 0

I am trying to understand what issues we can face if we implement hashCode() method wrongly.

For example, I tried to create a sample class HashEx which statically returns same hash value (100) for all the instances of the class and then tried to use HashEx in HashSet/HashMap with different operations:

HashSet -> add,read,contains
HashMap -> put,get

So far, all the operations seem to be working good. Any thoughts on this wild idea? I am trying to understand where this wrong implementation of hashCode() will create issues?

public class HashEx {

    public int id;
    public String name;

    public static void main(String[] args){

        HashEx e1 = new HashEx();
        e1.id=1;
        e1.name="Tom";

        HashEx e2 = new HashEx();
        e2.id=2;
        e2.name="Jerry";

        // set
        HashSet<HashEx> myset = new HashSet<HashEx>();
        myset.add(e1);
        myset.add(e2);

        System.out.println("Set size : "+ myset.size());
        for(HashEx e : myset){
            System.out.println("id: " + e.id + ", name: " + e.name);
        }

        HashEx e4 = new HashEx();
        e4.id = 2;
        e4.name = "Jerry";

        System.out.println("myset.contains(e4) : " + myset.contains(e4));

        // map
        HashMap<HashEx, String> map = new HashMap<HashEx, String>();

        map.put(e1, "Tom");
        map.put(e2, "Jerry");

        System.out.println("Map size : "+ map.size());
        System.out.println(map.get(e1));
        System.out.println(map.get(e2));
    }

    @Override
    public boolean equals(Object obj) {
        if(((HashEx)obj).id != id)
            return false;
        if(!((HashEx)obj).name.equals(name))
            return false;
        return true;
    }

    @Override
    public int hashCode() {
        return 100;
    }
}
  • 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-07T04:25:06+00:00Added an answer on June 7, 2026 at 4:25 am

    Everything will work properly (so long as you correctly implement equals(Object) in your HashEx class) in the sense that you won’t see any incorrect behavior.

    But when you get a large number of those objects in your HashSet (or as keys in a HashMap), then you will start seeing very poor performance. The objects are put into buckets based on their hashCode, and all the objects in the same bucket have to be searched linearly whenever one of the collection operations is done.

    So a better test to demonstrate the problem would be write a loop that just starts adding more and more objects (until the program runs out of memory or you kill it) and print out a status message every 10,000 objects. You’ll see that the add operation gets slower and slower (quadratically).

    If the objects instead have a different hashCode then the operation will not slow down (much) at all and it’ll run out of memory much faster.

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

Sidebar

Related Questions

I'm trying to understand why the second example below works with no issues, but
I'm trying to understand some memory issues concerning AVAudioPlayer and AVAudioRecorder. I create an
Trying to understand the options for will_paginate's paginate method: :page — REQUIRED, but defaults
Im trying to understand how class generics work and this bit just doesnt make
Can someone help me understand the WPF stretch alignment. I often run into issues
I am experiencing several issues that I can't understand from the first glances. The
I am running into issues trying to use large objects in R. For example:
I am trying to understand how my softphone issues BYEs to hangup so I
I'm doing some experiments, trying to understand the issues surrounding returning generics. The following
I have a somehow funny issue. While trying to understand why a certain website

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.