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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:00:54+00:00 2026-05-23T02:00:54+00:00

I need to filter an ArrayList and remove found elements. Being relatively new to

  • 0

I need to filter an ArrayList and remove found elements. Being relatively new to Java, I’m wondering what the most efficent method is to achieve this (matters because it runs on mobile devices). Currently I do this:

// We display only top-level dealers (parentId=-10)
ArrayList<DealerProductCount> subDealers = new ArrayList<DealerProductCount>();
for (DealerProductCount dealer : wsResponse.Dealers) {
    if (dealer.ParentId != -10) subDealers.add(dealer);
}
wsResponse.Dealers.removeAll(subDealers);

Can it be done without temporary objects? Maybe by directly manipulating (removing) elements of the list being iterated?

  • 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-23T02:00:55+00:00Added an answer on May 23, 2026 at 2:00 am

    Efficiently removing a number of elements from an ArrayList requires some thought. The naive approach is something like this:

    Iterator<DealerProductCount> it = wsResponse.Dealers.iterator();
    while (it.hasNext()) {
        if (it.next().ParentId != -10) { 
            it.remove(); 
        }
    }
    

    The problem is that each time you remove an element you copy (on average) half of the remaining elements. This is because removing an element from an ArrayList entails copying all elements after element removed one position to the left.

    Your original solution involving the list of elements to be removed essentially does the same thing. Unfortunately, the properties of an ArrayList don’t allow removeAll to do better than the above.

    If you expect to remove a number of elements, the following is more efficient:

    ArrayList<DealerProductCount> retain =
            new ArrayList<DealerProductCount>(wsResponse.Dealers.size());
    for (DealerProductCount dealer : wsResponse.Dealers) {
        if (dealer.ParentId == -10) {
            retain.add(dealer);
        }
    }
    // either assign 'retain' to 'wsResponse.Dealers' or ...
    wsResponse.Dealers.clear();
    wsResponse.Dealers.addAll(retain);
    

    We are copying (almost) the entire list twice, so this should break even (on average) if you remove as few as 4 elements.


    It is interesting to note that functional programming languages / libraries typical support a filter method, and that can do this task in one pass through the list; i.e. a lot more efficiently. I think we can expect significant improvements if / when Java supports lambdas, and the collection APIs are enhanced to use them.

    UPDATE and with Java 8 lambdas and streams, we get them … for this use-case.

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

Sidebar

Related Questions

I need to filter an array to remove the elements that are lower than
I have an ArrayList , and I need to filter it (only to remove
I fairly new to regular expressions and need some help. I need to filter
OK here goes: Need to filter out duplicates and the ability to remove added
short: I need to filter all .java files and every META-INF folder from a
I need to filter out anchor tags in a string. For instance, Check out
I need to filter result sets from sql server based on selections from a
I need to filter alphabetic and non alphanumeric characters from a string to make
I need to filter out junk data in SQL (SQL Server 2008) table. I
I need a filter function for a project I'm working on. I am thinking

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.