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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:20:09+00:00 2026-05-25T12:20:09+00:00

I want to compute differences between collections. When using CollectionUtils.subtract() for custom comparison I

  • 0

I want to compute differences between collections. When using CollectionUtils.subtract() for custom comparison I need to override the object’s equals() method. But what if I need to compare collections of objects of the same type but different comparison criterion? What about the Comparator interface, it seems perfectly suited here? AFAIK Comparator is mainly used for sorting. Isn’t there a method that uses Comparators for subtracting?

  • 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-25T12:20:10+00:00Added an answer on May 25, 2026 at 12:20 pm

    If you have an ArrayList, multiple removes can be more expensive than taking a copy.

    List<Type> list = /* ArrayList */
    Set<Type> toRemove = /* HashSet */
    List<Type> copy = new ArrayList<Type>(list.size());
    for(Type t: list)
      if(!toRemove.contains(t))
        copy.add(t);
    list = copy;
    

    Personally I would use a loop. Its likely to be shorter and clearer.

    Collection<Type> collection =
    
    for(Iterator<Type> i=collection.iterator(); i.hasNext();)
       if (i.next() is to be removed)
           i.remove();
    

    The reason an Iterator is used explicitly is to use the Iterator.remove() which avoids a ConcurrentModificationException. Another way to avoid it is to use a copy of the collection which might be preferred.

    for(Type t : new ArrayList<Type>(collection))
       if (t is to be removed)
           collection.remove(t);
    

    This doesn’t perform as well but may perform well enough.

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

Sidebar

Related Questions

I need to be able to compute differences between two data/time values which represent
What are the differences between SQLite and DISQLite and why would I want to
I want to compute the time 4 hours later during the DST transition, either
For use in a rigid body simulation, I want to compute the mass and
I want to ask how to add a computed column in BIRT that compute
I am using CoreGraphics to draw a quadratic bezier but want to computer the
I want to compute and count all trigrams of a string in SQL Server.
I want to calculate the average CPU usage % between two points of time.
I have a matrix M thats's 16384 x 81 . I want to compute
Given an array A[1..n] , we want to compute another array B[1..n] such 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.