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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:53:28+00:00 2026-06-03T14:53:28+00:00

Java allows the calculation of the (set theoretic) difference and the intersection of two

  • 0

Java allows the calculation of the (set theoretic) difference and the intersection of two Collection objects, via the removeAll() and retainAll() methods of the Collection interface.

The implementation of these 2 methods in the AbstractCollection class of Java 6 is

public boolean removeAll(Collection<?> c) { // Difference
boolean modified = false;
Iterator<?> e = iterator();
while (e.hasNext()) {
    if (c.contains(e.next())) {
    e.remove();
    modified = true;
    }
}
return modified;
}

public boolean retainAll(Collection<?> c) { // Intersection
boolean modified = false;
Iterator<E> e = iterator();
while (e.hasNext()) {
    if (!c.contains(e.next())) {
    e.remove();
    modified = true;
    }
}
return modified;
}

Is there any way of implementing or executing the above (obviously expensive) operations faster?

For example, would there be any overall performance gain from sorting a Collection before calculating the differences or the intersection?

Is there any class of the Collections framework preferable (performance-wise) for using these operations?

  • 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-06-03T14:53:30+00:00Added an answer on June 3, 2026 at 2:53 pm

    Yes, there is a faster method possible. The code you supplied loops through c for every element of e. With two arrays of 100 elements, it would compare approximately 100,000 elements.

    If you sort both arrays first, you only have to keep comparing the top two elements. This would do a couple hundred comparisons. This would be similar to merge sort. Do to do an intersection of the sorted collections left and right:

    function intersect(left, right)
        var list result
        while length(left) > 0 and length(right) > 0
            if first(left) == first(right)
                append first(left) to result
                left = rest(left)
                right = rest(right)
            else if first(left) < first(right)
                left = rest(left)
            else
                right = rest(right)
        end while
        return result
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just found out that Java allows enums to implement an interface. What would
Can anyone explain to me why java allows you to access static methods and
The following Java design allows an object to be extended without changing its interface
I have a graphics system for Java which allows objects to be wallpapered by
The java compiler allows me write a Class definition inside an Interface . Are
I am used to using JMock in Java which allows you to specify things
java.util.PriorityQueue allows a Comparator to be passed at construction time. When inserting elements, they
Is there any Java profiler that allows profiling short-lived applications? The profilers I found
I'm looking for a standalone Java library which allows me to parse LDAP style
Is there a Java web framework that allows the user to create custom fields,

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.