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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:09:17+00:00 2026-06-09T21:09:17+00:00

I need to implement the equals method in some class A. Class A has

  • 0

I need to implement the equals method in some class A. Class A has an orderer collection of Enum type, and the behaviour I want to achive is that equals returns true for two instances of Class A that have exactly the same Enum values in the collection (in exactly the same positions of the collection).

As I’m new to java, I’m having problems with this, and I dont know how to properly implement equals or the hashcode methods, so any help would be good 🙂

  • 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-09T21:09:18+00:00Added an answer on June 9, 2026 at 9:09 pm

    If you’re using eclipse (netbeans has similar features, as do most java IDEs), you can simply got to the “Source” menu, and choose “Generate hashcode() and equals()”. Then you select the fields you want to be considered (in your case the list of enum values.

    That being said, assuming you already have the enum, here’s the code that eclipse generated for me. Not that hashcode usually involves a prime number, as well as multiplication and addition. This tends to give you somewhat decent distribution of values.

    public class Foo {
       private List<FooEnum> enumValues;
    
       @Override
       public int hashCode() {
           final int prime = 31;
           int result = 1;
           result = prime * result
                   + ((enumValues == null) ? 0 : enumValues.hashCode());
           return result;
       }
    
       @Override
       public boolean equals(Object obj) {
           if (this == obj)
               return true;
           if (obj == null)
               return false;
           if (getClass() != obj.getClass())
               return false;
           Foo other = (Foo) obj;
           if (enumValues == null) {
               if (other.enumValues != null)
                   return false;
           }
           else if (!enumValues.equals(other.enumValues))
               return false;
           return true;
       }
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to implement equals() method for an Item that may be put in
This is a question about how to implement the equals method when I need
I need to implement some new functions on an editor. I picked Emacs -
i see that the IEqualityComparer interface has Equals(obj x, obj y) and GetHashcode(). I
I need to implement 3 public methods that all rely on the same initial
I need to implement a bag data structure using the interface java.util.Collection . I'm
I need to override Equals() method for one of my types but it seems
For a project, I need to replicate some results that currently exist in Stata
What I need suggestions for is the Equals method located at the near the
I need to implement portable code, but I do not know how to deal

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.