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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:10:21+00:00 2026-05-20T11:10:21+00:00

I want to add objects of a custom type to a Set. I have

  • 0

I want to add objects of a custom type to a Set. I have several which are the same, i.e. they have the same values for their public variables.

I don’t want more than one instance of a “same” object to be added to the set, but each time a new object is created it is always added.

This is because the equals method for class Object implements the most discriminating possible equivalence relation on objects: “For any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).”

Can I override the equals method for this object to define it differently?

Thanks everyone the problem is solved

Sameness for java objects is defined by overriding the Java Object’s equals() method.

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((uri == null) ? 0 : uri.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (!(obj instanceof Resource))
        return false;
    Resource other = (Resource) obj;
    if (uri == null) {
        if (other.uri != null)
            return false;
    } else if (!uri.equals(other.uri))
        return false;
    return true;
}
  • 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-20T11:10:22+00:00Added an answer on May 20, 2026 at 11:10 am

    You should override the equals, and hashCode methods for your custom type.

    Be careful though, you can get into all sorts of shit here: for instance if you have sub-types for your custom type, you can run into other equality problems:

    class Point {
        final int x;
        final int y;
    
        public Point(int x, int y) {
            this.x= x;
            this.y = y;
        }
    
        @Override
        public boolean equals(Object o) {
            if (o == this) return true;   //If objects equal, is OK
            if (o instanceof Point) {
               Point that = (Point)o;
               return (x == that.x)  && y == that.y);
            }
            return false;
        }
    }
    
    class ColoredPoint extends Point {
       final Color color;
       ColoredPoint(int x, int y, Color color) {
          super(x, y);
          this.color = color
       }
    }
    
    Point p1 = new Point(1, 2);
    ColoredPoint cp1 = new ColoredPoint(1, 2, Color.BLUE);
    ColoredPoint cp1 = new ColoredPoint(1, 2, Color.RED);
    

    As it stands, p1, cp1, and cp2 are all equal. However, obviously cp1 and cp2 are not equal. You’ll need to implement an equals in ColoredPoint too, to compare ColoredPoint Objects (but this breaks equality between p1 and cp1, or cp2).

    Also, make sure your equals has the signature above. It’s a common mistake to define it as public equals(Point that)..., which is wrong.

    See http://www.artima.com/lejava/articles/equality.html for a full explanation of the rules for equals and hashCode

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

Sidebar

Related Questions

I have a set of objects of type Idea public class Idea { public
I have an array of objects and I want to add another object just
I have a situation where i want to add LinePragmas to CodeDom objects. But
I have a number of custom objects of type X. X has a number
I have a List<> of custom objects. This custom type has a property called
I have a custom type MyClass and a factory class Factory that creates objects
I have a custom c# type like (just an example): public class MyVector {
I want to have a nested table holding custom objects, by adding them one
I want to add as many labels as objects in my array. but how
I have this script where I want to add an object to an array

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.