Return a set containing all elements that are both in source and in query Delete these elements from source - Java java.util

Java examples for java.util:Set Intersection

Description

Return a set containing all elements that are both in source and in query Delete these elements from source

Demo Code


//package com.book2s;
import java.util.*;

public class Main {
    /**//  ww w.  ja  va2  s. c om
     * Answer a set containing all elements that are both in source and in query
     * Delete these elements from source Creation date: (13.10.2002 16:23:00)
     * 
     * @return java.util.Set
     * @param source
     *            java.util.Set
     * @param elements
     *            java.util.Collection
     */
    public static Set extractFrom(Set source, Collection query) {
        Set answer = new HashSet();
        Iterator i = query.iterator();
        while (i.hasNext()) {
            Object o = i.next();
            if (source.remove(o)) {
                answer.add(o);
            }
        }
        return answer;
    }
}

Related Tutorials