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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:04:04+00:00 2026-05-25T10:04:04+00:00

Suppose I have the below class. class S{ String txt = null; S(String i){

  • 0

Suppose I have the below class.

class S{

String txt = null;

S(String i){
txt=i;
}

public static void main(String args []){
S s1 = new S("a");
S s2 = new S("b");
S s3 = new S("a"); 

Map m = new HashMap ();
m.put(s1, "v11");
m.put(s2, "v22");
m.put(s3, "v33");

System.out.println(m.size());   
}

//just a plain implementation
 public boolean equals(Object o)
 {
      S cc = (S) o;
      if (this.i.equals(cc.i))
      {    
         return true;
      }
      else
      {
         return false;
      }    
   }

   public int hashCode()
   {
      return 222;
   }  

}

This will return size as 2 when running above. Its totally fine. If we comment the hashCode() it return 3 which is also correct. But if we comment the equals and keep the hashCode it should return 2 right? instead it returns 3. When putting objects to hashmap map will check the hash code of an object and if its same it will replace the previous value of the map to the new one right?

Thank You.

  • 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-25T10:04:04+00:00Added an answer on May 25, 2026 at 10:04 am

    But if we comment the equals and keep the hashCode it should return 2
    right? instead it returns 3.

    3 items is the correct behaviour. 3 objects will be hashed to the same bucket, but because all 3 are different this bucket will contain a chain of values (linked list for HashMap in Java) with the same hash code but not equal to each other.

    When putting objects to hashmap map will check the hash code of an object and if its same
    it will replace the previous value of the map to the new one right?

    If they are hashed to the same bucket it doesn’t mean that one value will replace another. Then these values will be compared for equality. If they are equal then old value will be replaced, if they are not – new value will be added to the tail of the linked list (for this bucket).

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

Sidebar

Related Questions

Assumptions Suppose I have a class with a property: class ClassWithProperty { public string
Suppose I have the elements as below: <div class="home"> <div class="tab231891230"></div> <div class="tab121232441"></div> <div
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose I have a class module clsMyClass with an object as a member variable.
Suppose I have BaseClass with public methods A and B, and I create DerivedClass
Suppose we have: interface Foo { bool Func(int x); } class Bar: Foo {
I have an abstract class CommandPath, and a number of derived classes as below:
I suppose that this is an interesting code example. We have a class --
I have the below code which is suppose to show a list of contact
i have a List<my_Custom_Class> and List<string> . My custom class has various items in

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.