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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:55:04+00:00 2026-05-13T15:55:04+00:00

I have a code to return an arrayList with the duplicates of an ArrayList

  • 0

I have a code to return an arrayList with the duplicates of an ArrayList
but seems it’s not working, I am comparing all items in the array…

public ArrayList<ObjectList> duplicates(ArrayList<ObjectList> someObjectsList) {

    ArrayList<ObjectList> ret = new ArrayList<ObjectList>();
    for ( ObjectList aSomeObjectsList: someObjectsList) {

        String field1 = aSomeObjectsList.get1();
        String field2 = aSomeObjectsList.get2();
        String field3 = aSomeObjectsList.get3();
        String field4 = aSomeObjectsList.get4();
        for (ObjectList someObject : ret) {
            if (
                field1.trim().equals(someObject.get1())&& 
                field2.trim().equals(someObject.get2())&&
                field3.trim().equals(someObject.get3())&&
                field4.trim().equals(someObject.get4())     
                ){
                ret.add(aSomeObjectsList);

            }
        }

    }
    return ret;
} 

But i guess I am doing something wrong because it doesn’t return anything, and I know it has duplictates under this 4 field criteria

Thanks in advance

  • 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-13T15:55:05+00:00Added an answer on May 13, 2026 at 3:55 pm

    Here’s a way you can do this. I have defined a basic class ObjectList that shows a way to implement equals and hashCode. Note that this assumes that all the internal variables are non-null. If these variables can contain null then you will need to check for that when computing the equals/hashCode. Also, the objects in this class must also themselves properly implement equals/hashCode.

    public class ObjectList {
    
        private int h;
    
        private Object obj1;
        private Object obj2;
        private Object obj3;
        private Object obj4;
    
        @Override
        public boolean equals(final Object o) {
            if (!(o instanceof ObjectList))
                return false;
    
            final ObjectList that = (ObjectList) o;
            return that.obj1.equals(obj1) && that.obj2.equals(obj2)
                && that.obj3.equals(obj3) && that.obj4.equals(obj4);
        }
    
        @Override
        public int hashCode() {
            // caches the hashcode since it could be costly to recompute every time
            // but this assumes that your object is essentially immutable 
            // (which it should be if you are using equals/hashCode. If this is not
            // true and you want to just temporarily use this when doing the duplicate
            // test, move the h variable definition from the object level to this method
            // and remove this if statement.
            if (h != 0)
                return h;
    
            h = obj1.hashCode();
            h = h * 31 + obj2.hashCode();
            h = h * 31 + obj3.hashCode();
            h = h * 31 + obj4.hashCode();
            return h;
        }
    
    }
    
    public Collection<ObjectList> duplicates(
            final Collection<ObjectList> someObjectsList) {
    
        final Set<ObjectList> unique = new HashSet<ObjectList>(someObjectsList);
        final ArrayList<ObjectList> ret = new ArrayList<ObjectList>(someObjectsList);
        for (final ObjectList o : unique) {
            ret.remove(o);
        }
    
        // The ret list now contains the duplicate instances; instances 
        // with more than two occurrences will occur multiple times still in
        // this list.
        return ret;
    
        // If you want a list of unique duplicate instances then, comment out the above
        // return and uncomment this one.
        // return new HashSet<ObjectList>(ret);
    }
    

    Using Collection<ObjectList> is better, if you can do that, for both the parameter and returned value so you can vary the implementations (ArrayList, Set, etc).

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

Sidebar

Ask A Question

Stats

  • Questions 443k
  • Answers 443k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Having the same problem. Removed the -q option from the… May 15, 2026 at 6:14 pm
  • Editorial Team
    Editorial Team added an answer There is no context as such except for sa inside… May 15, 2026 at 6:14 pm
  • Editorial Team
    Editorial Team added an answer In your case, the $ps variable created by pspell_new() is… May 15, 2026 at 6:14 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.