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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:05:57+00:00 2026-05-15T04:05:57+00:00

Is there a method in JDK or apache commons to pop a list of

  • 0

Is there a method in JDK or apache commons to “pop” a list of elements from a java.util.List? I mean, remove the list of elements and return it, like this method:

public Collection pop(Collection elementsToPop, Collection elements) {

  Collection popped = new ArrayList();

  for (Object object : elementsToPop) {
    if (elements.contains(object)) {
      elements.remove(object);
      popped.add(object);
    }
  }

  return popped;
}
  • 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-15T04:05:58+00:00Added an answer on May 15, 2026 at 4:05 am

    If you’re looking for a stack-like structure I suggest accepting a Deque (LinkedList is the most common implementation) instead of a Collection.

    If you don’t actually need to treat it as a stack, just get an iterator from the Collection and use the remove() method:

    for (Iterator<SomeType> it = elements.iterator(); it.hasNext(); ) {
        SomeType e = it.next();
        it.remove();
        popped.add(e);
    }
    

    Do note that remove is an optional operation, and some implementations may throw an UnsupportedOperationException (for example, the iterator returned by a Collection from Collections.unmodifiable...() will).

    Edit: After looking more closely at your question, I think you just need this:

    elements.removeAll(elementsToRemove);
    

    If your main point is you need to know exactly which elements were actually popped, I think you’re stuck with your original code.

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

Sidebar

Related Questions

Is there in the JDK or Jakarta Commons (or anywhere else) a method that
Is there a more concise/standard idiom (e.g., a JDK method) for piping an input
Is there a simpler way of implement this? Or a implemented method in JDK
Is there any method to generate MD5 hash of a string in Java?
Is there a method to launch an Internet Explorer window from a desktop Icon
The JDK's String.trim() method is pretty naive, and only removes ascii control characters. Apache
I am trying to use the FileWalkTree() method in JDK 7 (java.nio.file) . To
We used a private method, through reflection, of a JDK class. Is there a
auto generated method argument names looks like arg0, arg1, arg2 is there any way
I'm having a play with the Java NIO.2 API from JDK 7. In particular,

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.