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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:02:14+00:00 2026-05-20T18:02:14+00:00

I have an (immutable) object, Group, which I am trying to use in a

  • 0

I have an (immutable) object, Group, which I am trying to use in a HashSet. However, I am getting odd results:

// Position is another immutable class, GroupType is an enum
Group t1 = new Group(new Position(0, 0), GroupType.ROW);
Group t2 = new Group(new Position(0, 0), GroupType.ROW);
Set<Group> s = new HashSet<Group>();
s.add(t1);
System.out.format("t1.hashCode(): %d\nt2.hashCode(): %d\nt1.hashCode() == t2.hashCode(): %b\nt1.equals(t2): %b\nt2.equals(t1): %b\ns.contains(t1): %b\ns.contains(t2): %b\n",
    t1.hashCode(),
    t2.hashCode(),
    t1.hashCode() == t2.hashCode(),
    t1.equals(t2),
    t2.equals(t1),
    s.contains(t1),
    s.contains(t2)
    );

results in the following:

t1.hashCode(): 486656595
t2.hashCode(): 486656595
t1.hashCode() == t2.hashCode(): true
t1.equals(t2): true
t2.equals(t1): true
s.contains(t1): true
s.contains(t2): false

t1 and t2 have identical hashcodes, and equals() claims that they are the same. How can the HashSet contain one and not the other?

(And no, none of those methods are secretly modifying t1 or t2; repeating the print statement garners the same results.)

Group.equals() is as follows:

public boolean equals(Group g2) {
    return (this.type.equals(g2.type)) && (this.basis.equals(g2.basis));
}

type is a (final) enum stored. basis is a Position, which has the following equals:

public boolean equals(Position pos) {
    return (x == pos.x) && (y == pos.y);
}

where x and y are internal, final variables.

However, I get the same results replacing it with:

public boolean equals(Group g2) {
    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-20T18:02:14+00:00Added an answer on May 20, 2026 at 6:02 pm

    I have a suspicion… my guess is that your equals method looks like this:

    public boolean equals(Group other)
    

    That’s not overriding the built-in equals method, which is what HashSet will use. Make sure your equals method is:

    @Override // Make the compiler check we're overriding something
    public boolean equals(Object other)
    

    Another way of testing this is:

    Object o1 = t1;
    Object o2 = t2;
    
    System.out.println(o1.equals(o2));
    System.out.println(o2.equals(o1));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class which is intended for immutable use, hence I would like
I have the following attempt at an immutable object: class MyObject { private static
When I have an immutable parent class A that is NOT final, and another
I have designed a class containing some information about a given object which will
I know that TimeSpan's are immutable. I have a object which contains a TimeSpan
I want to have immutable Java objects like this (strongly simplified): class Immutable {
I have an immutable class with some private fields that are set during the
Lets say have this immutable record type: public class Record { public Record(int x,
I have come across a class that has an immutable property: MyObject[] allObjs The
I have a class for products import from CSV file operation which requires about

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.