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

The Archive Base Latest Questions

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

I was trying to use set. so i tried this way HashSet set=new HashSet();

  • 0

I was trying to use set. so i tried this way

HashSet set=new HashSet();
Points p1 = new Points(10, 20);
Points p2 = new Points(10, 20);
System.out.println(set.add(p1)); // output true
System.out.println(set.add(p2));  // output false

I know my first output will be true and second will be false as Set will not allow duplicate elements. And, i also know Set achieve this by using equals(Object o) method. Which comes from java Object class with following signature.

public boolean equals(Object o) {}

For testing this little bit deeper i wrote my own class

class testSet{
    private int x;
    private int y;
    public testSet(int xa, int ya){
        this.x=xa;
        this.y=ya;
    }
    @Override
    public boolean equals(Object o){
        if (o instanceof testSet){
            testSet ts=(testSet)o;
            return ts.x==this.x && ts.y==this.y;
        }
        return false;
    }
}

Now i am expecting following code will behave same as Point class

HashSet set=new HashSet();
testSet set_a=new testSet(3,4);
testSet set_b=new testSet(3,4);
System.out.println(set.add(set_a)); //output expected true
System.out.println(set.add(set_b)); //output expected false

So i am expecting first output will be true and second one false . But it is always returning true for both case. But did working for Point class. And i tried with following two Point implementations

android.graphics.Point
java.awt.point 

So what i did wrong? Thanks for helping guys.

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

    You need to override hashCode as well as equals. The general contract of hashCode is that if two objects are equal (in the sense of equals), then they have the same hash code.

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

Sidebar

Related Questions

I am trying to use this MySQL query: SET @a:=0; UPDATE tbl SET sortId=@a:=@a+1
I am trying to use set.insert (key) as a conditional, where if the key
I was trying use a set of filter functions to run the appropriate routine,
I'm trying to use ResolveUrl() to set some paths in the code behind of
I' trying to use a Linq query to find and set the selected value
I'm trying to use an array to set the where parameters for a Zend
I am trying to use the win32 API to set the parent of an
I'm trying to use the show and hide to display a different set of
I'm trying to use openmp to multithread a loop through std::set. When I write
I'm trying to use mock to verify that an index property has been set.

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.