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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:01:46+00:00 2026-06-18T11:01:46+00:00

Possible Duplicate: Overriding equals and hashCode in Java I am supposed to implement hashcode

  • 0

Possible Duplicate:
Overriding equals and hashCode in Java

I am supposed to implement hashcode and equals for Custom class Person. Person consists of

firstname

lastname

I am supposed to implement equals and hashcode such that two people with firstname and lastname should return true for equals and should be accepted by Hashmap. I have implemented Person class like this:

public class Person {


    String firstname;
    String lastname;
    public Person(String firstname, String lastname) {
        this.firstname = firstname;
        this.lastname = lastname;
    }

    @Override
    public int hashCode() {
        // TODO Auto-generated method stub
        return firstname.hashCode()+lastname.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        // TODO Auto-generated method stub
    Person u=(Person) obj;

        return u.firstname.equals(firstname) && u.lastname.equals(lastname);
    }

}

Is the implementation of Hashcode correct here? Even though I am getting the expected result,I want to know if this implementation is correct?

  • 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-18T11:01:47+00:00Added an answer on June 18, 2026 at 11:01 am

    There is a slight problem with your equals method because it will throw an exception if obj is null or not a Person, so you should add the following to the top of your equals:

    if(obj==null || !(obj instanceof Person))
       return false;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Overriding equals and hashCode in Java If I have class A {
Possible Duplicate: Overriding equals and hashCode in Java All, I have defined my class
Possible Duplicate: In Java, why must equals() and hashCode() be consistent? I read that
Possible Duplicate: Returning false from Equals methods without Overriding One of my colleague asked
Possible Duplicate: Overriding parent class’s function I'm struggling with calling a virtual function in
Possible Duplicate: operator overloading and overriding in java I want to know if it
Possible Duplicate: Why doesn't Java allow overriding of static methods? Is there any legitimate
Possible Duplicate: What's the difference between IEquatable and just overriding Object.Equals() ? I know
Possible Duplicate: Overriding the power button in Android Is there any possible way I
Possible Duplicate: Overriding static variables when subclassing I have a set of classes that

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.