Merge the new set so the set of sets remains disjoint - Java java.util

Java examples for java.util:Set Operation

Description

Merge the new set so the set of sets remains disjoint

Demo Code

/*//from  ww  w  .j a  va 2s.  c  o  m
 **    Copyright (C) 2003-2012 Institute for Systems Biology 
 **                            Seattle, Washington, USA. 
 **
 **    This library is free software; you can redistribute it and/or
 **    modify it under the terms of the GNU Lesser General Public
 **    License as published by the Free Software Foundation; either
 **    version 2.1 of the License, or (at your option) any later version.
 **
 **    This library is distributed in the hope that it will be useful,
 **    but WITHOUT ANY WARRANTY; without even the implied warranty of
 **    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 **    Lesser General Public License for more details.
 **
 **    You should have received a copy of the GNU Lesser General Public
 **    License along with this library; if not, write to the Free Software
 **    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
import java.util.Collection;
import java.util.Collections;
import java.util.SortedSet;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.net.URL;
import java.net.URLConnection;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{
    /***************************************************************************
     **
     ** Merge the new set in, coalese so the set of sets remains disjoint
     */

    public static Set coaleseSets(Set setOfSets, Set newSet) {

        //
        // Merge together.  In general, the sets in the set are
        // no longer disjoint.
        //
        HashSet result = new HashSet(setOfSets);
        result.add(newSet);

        //
        // Get a fixed ordering of all set members.
        //

        HashSet allMembers = new HashSet();
        Iterator rit = result.iterator();
        while (rit.hasNext()) {
            Set nextSet = (Set) rit.next();
            allMembers.addAll(nextSet);
        }
        ArrayList fixedOrder = new ArrayList(allMembers);

        //
        // For each member, if it appears in two sets,
        // we extract those sets from the result, and
        // return the union.  Continue this until the
        // scan is clean for all members!
        //

        Iterator smit = fixedOrder.iterator();
        while (smit.hasNext()) {
            Object member = (Object) smit.next();
            boolean done = false;
            while (!done) {
                Set firstSet = null;
                boolean collision = false;
                Iterator rsit = result.iterator();
                while (rsit.hasNext()) {
                    Set nextSet = (Set) rsit.next();
                    if (nextSet.contains(member)) {
                        if (firstSet == null) {
                            firstSet = nextSet;
                        } else {
                            result.remove(nextSet);
                            result.remove(firstSet);
                            HashSet union = new HashSet();
                            union(nextSet, firstSet, union);
                            result.add(union);
                            collision = true;
                            break;
                        }
                    }
                }
                if (!collision) {
                    done = true;
                }
            }
        }
        return (result);
    }
    /***************************************************************************
     **
     ** Set union helper
     */

    public static Set union(Set one, Set two, Set result) {
        result.clear();
        result.addAll(one);
        result.addAll(two);
        return (result);
    }
}

Related Tutorials