How to pop items from a collection in Java?

Posted by Tom Brito on Stack Overflow See other posts from Stack Overflow or by Tom Brito
Published on 2010-06-08T15:36:31Z Indexed on 2010/06/08 15:52 UTC
Read the original article Hit count: 186

Filed under:
|

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;
}

© Stack Overflow or respective owner

Related posts about java

Related posts about collections