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

  • SEARCH
  • Home
  • 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 7407853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:48:09+00:00 2026-05-29T05:48:09+00:00

Consider adding an equality method to the following class of simple points: public class

  • 0

Consider adding an equality method to the following class of simple points:

public class Point {

    private final int x;
    private final int y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    // ...
}

// my definition of equals

public boolean equals(Point other) {
  return (this.getX() == other.getX() && this.getY() == other.getY());
}

What’s wrong with this method? At first glance, it seems to work OK:

Point p1 = new Point(1, 2);
Point p2 = new Point(1, 2);

Point q = new Point(2, 3);

System.out.println(p1.equals(p2)); // prints true

System.out.println(p1.equals(q)); // prints false

However, trouble starts once you start putting points into a collection:

import java.util.HashSet;

HashSet<Point> coll = new HashSet<Point>();
coll.add(p1);

System.out.println(coll.contains(p2)); // prints false

How can it be that coll does not contain p2, even though p1 was added to it, and p1 and p2 are equal objects?

  • 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-29T05:48:10+00:00Added an answer on May 29, 2026 at 5:48 am

    While it is true that you should implement hashCode() when you implement equals(), that is not causing your problem.

    That is not the equals() method you are looking for. The equals method must always have the following signature: “public boolean equals(Object object)”. Here is some code.

    public boolean equals(Object object)
    {
      if (object == null)
      {
        return false;
      }
    
      if (this == object)
      {
        return true;
      }
    
      if (object instanceof Point)
      {
        Point point = (Point)object;
        ... now do the comparison.
      }
      else
      {
         return false;
      }
    }
    

    The Apache EqualsBuilder class is useful for equals implementations. The link is an older version, but still applicable.

    If you liked the Apache EqualsBuilder, you will probably like the Apache HashCodeBuilder class as well.

    Edit: updated the equals method example for standard shortcuts.

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

Sidebar

Related Questions

Consider this demo program: #include <stdio.h> class Base { public: virtual int f(int) =0;
consider the following class and struct public class Entity { public IdType Id {get;set;}
Consider the following code: public abstract class Test1 { public object Data { get;
Consider these classes. class Base { ... }; class Derived : public Base {
Consider the below C++ code class B; class A{ private: B* mB; }; class
Consider the following code: public ObservableCollection<Message> Messages { get { return new ObservableCollection<Message>(); }
consider this simple code: echo iconv('UTF-8', 'ASCII//TRANSLIT', 'è'); it prints `e instead of just
Consider a custom UIComponent (for test purposes only): public class UITest extends UIComponentBase {
Given the following CRTP type in C#: public abstract class DataProviderBase<TProvider> where TProvider :
Consider the following table with a constraint that's a bit daft but simple enough

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.