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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:04:43+00:00 2026-06-01T06:04:43+00:00

I produce a bunch of objects in Java. Each object has attribute area and

  • 0

I produce a bunch of objects in Java. Each object has attribute area and a set of integers. I want to store those objects for example in a map(keys should be integers in a growing order). Two objects are the same if their area is equal and their sets are the same.

If two objects don’t have the same area then there is no need for me to check whether their sets are the same.

What is the best practice for implementing this in Java? How should I compose hash and equal functions?

  • 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-01T06:04:44+00:00Added an answer on June 1, 2026 at 6:04 am

    Here’s sample pair of hashCode\equals generated by IDE:

    class Sample  {
        final int area;
        final Set<Integer> someData;
    
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
    
            Sample sample = (Sample) o;
    
            if (area != sample.area) return false;
            if (!someData.equals(sample.someData)) return false;
    
            return true;
        }
    
        @Override
        public int hashCode() {
            int result = area;
            result = 31 * result + someData.hashCode();
            return result;
        }
    }
    

    This code assumes someData can’t be null — to simplify things. You can see that equality of types is checked at first, then area equality is checked and then equality of Set<Integer> is checked. Note that built-in equals of Set is used in this — so you have re-usage of that method. This is idiomatic way to test compound types for equality.

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

Sidebar

Related Questions

I'm trying to produce a report that has multiple grouping but does not just
So I've got a bunch of worker threads doing simple curl class, each worker
I'm serializing a bunch of objects with: json = serializers.serialize(json, objects, fields=('name', 'country')) I
I'm using partcover to produce codecoverage for a bunch of mstest powered unit tests.
This has probably been asked a lot, and I see a whole bunch of
I am trying to filter a bunch of objects through a many-to-many relation. Because
I am trying to filter a bunch of objects through a many-to-many relation. Because
I'm needing to produce a checksum for a bunch of data in Perl, and
I have a bunch of Yaml configuration files that I want to convert to
Like many 3d graphical programs, I have a bunch of objects that have their

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.