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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:26:34+00:00 2026-05-23T19:26:34+00:00

This is a pretty common question, but I could not find this part: Say

  • 0

This is a pretty common question, but I could not find this part:

Say I have this array list:

List<MyDataClass> arrayList = new List<MyDataClass>;

MyDataClass{
   String name;
   String age;
}

Now, I need to find duplicates on the basis of age in MyDataClass and remove them. How is it possible using something like HashSet as described here?

I guess, we will need to overwrite equals in MyDataClass?

  1. But, what if I do not have the luxury of doing that?
  2. And How does HashSet actually internally find and does not add duplicates? I saw it’s implementation here in OpenJDK but couldn’t understand.
  • 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-23T19:26:34+00:00Added an answer on May 23, 2026 at 7:26 pm

    I’d suggest that you override both equals and hashCode (HashSet relies on both!)

    To remove the duplicates you could simply create a new HashSet with the ArrayList as argument, and then clear the ArrayList and put back the elements stored in the HashSet.

    class MyDataClass {
        String name;
        String age;
    
        @Override
        public int hashCode() {
            return name.hashCode() ^ age.hashCode();
        }
    
        @Override
        public boolean equals(Object obj) {
            if (!(obj instanceof MyDataClass))
                return false;
    
            MyDataClass mdc = (MyDataClass) obj;
            return mdc.name.equals(name) && mdc.age.equals(age);
        }
    }
    

    And then do

    List<MyDataClass> arrayList = new ArrayList<MyDataClass>();
    
    Set<MyDataClass> uniqueElements = new HashSet<MyDataClass>(arrayList);
    arrayList.clear();
    arrayList.addAll(uniqueElements);
    

    But, what if I do not have the luxury of doing that?

    Then I’d suggest you do some sort of decorator-class that does provide these methods.

    class MyDataClassDecorator {
    
        MyDataClass mdc;
    
        public MyDataClassDecorator(MyDataClass mdc) {
            this.mdc = mdc;
        }
    
        @Override
        public int hashCode() {
            return mdc.name.hashCode() ^ mdc.age.hashCode();
        }
    
        @Override
        public boolean equals(Object obj) {
            if (!(obj instanceof MyDataClassDecorator))
                return false;
    
            MyDataClassDecorator mdcd = (MyDataClassDecorator) obj;
            return mdcd.mdc.name.equals(mdc.name) && mdcd.mdc.age.equals(mdc.age);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am sure that this must be a pretty common question but after scouring
I thought this would be a pretty common question to look for in Google
This may be a basic question but I am pretty new to DDD. I
I'm pretty sure that this is the right site for this question, but feel
This seems like a pretty common problem, but I haven't found any sort of
Unfortunately this is going to be a pretty open-ended question, but I am at
This is quite a common question but the solutions I found in other people
I find this pretty confusing. When you want to rotate a view, it's going
I have written something like this pretty easily in C# ( string GetUrl(new {
This is pretty weird. I have my Profiler open and it obviously shows that

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.