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

The Archive Base Latest Questions

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

I have a list of objects say, List. The Entity class has an equals

  • 0

I have a list of objects say, List. The Entity class has an equals method,on few attributes ( business rule ) to differentiate one Entity object from the other.

The task that we usually carry out on this list is to remove all the duplicates something like this :

List<Entity> noDuplicates = new ArrayList<Entity>();
for(Entity entity: lstEntities)
{
    int indexOf = noDuplicates.indexOf(entity);
    if(indexOf >= 0 )
    {
            noDuplicates.get(indexOf).merge(entity);
    }
    else
    {
            noDuplicates.add(entity);
     }
}

Now, the problem that I have been observing is that this part of the code, is slowing down considerably as soon as the list has objects more than 10000.I understand arraylist is doing a o(N) search.

Is there a faster alternative, using HashMap is not an option, because the entity’s uniqueness is built upon 4 of its attributes together, it would be tedious to put in the key itself into the map ? will sorted set help in faster querying ?

Thanks

  • 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-14T20:50:03+00:00Added an answer on May 14, 2026 at 8:50 pm

    Now, the problem that I have been observing is that this part of the code, is slowing down considerably as soon as the list has objects more than 10000.I understand arraylist is doing a o(N) search.

    The algorithm you posted is actually worse than O(N)

    • Iterating through the input list lstEntities – O(N)
    • within this loop, you are calling ArrayList.indexOf(T) which has to scan the list – O(N) again

    You algorithm is actually O(N^2) since you are potentially scanning the list twice within a loop.

    It sounds like you what you want to do is actually two operations:

    1. From the input List, remove any duplicates
    2. When you find duplicates, “merge” the entities.

    You can do this by scanning the list just once, rather than in nested loops. I would recommend breaking up your Entity to move the fields that “identify” an Entity into another type, such as ID, or at the very least add a getID() method which can return these fields grouped into a single type. This way you can easily build a Map between the two types to be able to merge entities with “duplicate” identities. This might look something like this:

    Map<ID, Entity> map = new HashMap<ID, Entity>(inputList.size());
    for (Entity e : inputList) {
        Entity existing = map.get(e.getID());
        if (existing == null) {
            //not in map, add it
            map.put(e.getID(), e);
        } 
        else {
            existing.merge(e);
        }
    }
    

    Iterating through the list is O(n) while HashMap.get(K) is a constant-time operation.

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

Sidebar

Related Questions

I have a list of objects (for the sake of example, let's say 5).
I have a list of bean objects passed into my JSP page, and one
Let's say I have an unsorted list of four objects: [B, C, A, D]
I have a parent object called Page that has a List of objects called
I have a strongly typed list of custom objects, MyObject , which has a
Let's say I have a List object and an iterator for that list. Now
I have a list of objects I wish to sort based on a field
I have a list of objects, each containing an Id, Code and Description. I
I have IQueryable list of objects of type T which I want to transform
I have a list of objects and I want to reorder them randomly on

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.