Java Vector Union union(Vector vectA, Vector vectB)

Here you can find the source of union(Vector vectA, Vector vectB)

Description

This method returns a Vector containing the union of the objects contained in vectA and vectB.

License

Open Source License

Declaration


public static Vector union(Vector vectA, Vector vectB) 

Method Source Code


//package com.java2s;
/*/*  ww w  .ja  v a2s  .co  m*/
    
  VectorUtils.java
    
  Convenience methods for working with Vectors.. provides efficient Union,
  Intersection, and Difference methods.
      
  Created: 21 July 1998
  Release: $Name:  $
  Version: $Revision: 1.21 $
  Last Mod Date: $Date: 2002/11/01 02:25:10 $
  Module By: Jonathan Abbey, jonabbey@arlut.utexas.edu
    
  -----------------------------------------------------------------------
          
  Ganymede Directory Management System
    
  Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
  The University of Texas at Austin.
    
  Contact information
    
  Web site: http://www.arlut.utexas.edu/gash2
  Author Email: ganymede_author@arlut.utexas.edu
  Email mailing list: ganymede@arlut.utexas.edu
    
  US Mail:
    
  Computer Science Division
  Applied Research Laboratories
  The University of Texas at Austin
  PO Box 8029, Austin TX 78713-8029
    
  Telephone: (512) 835-3200
    
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
    
  This program 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 General Public License for more details.
    
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    
*/

import java.util.*;

public class Main {
    /**
     * <P>This method returns a Vector containing the union of the objects
     * contained in vectA and vectB.  The resulting Vector will not
     * contain any duplicates, even if vectA or vectB themselves contain
     * repeated items.</P>
     *
     * <P>This method will always return a new, non-null Vector, even if
     * vectA and/or vectB are null.</P>
     */

    public static Vector union(Vector vectA, Vector vectB) {
        int threshold = 0;

        if (vectA != null) {
            threshold = vectA.size();
        }

        if (vectB != null) {
            threshold += vectB.size();
        }

        if (threshold < 10) // I pulled 10 out of my ass
        {
            Vector result = new Vector(threshold);

            if (vectA != null) {
                for (int i = 0; i < vectA.size(); i++) {
                    Object obj = vectA.elementAt(i);

                    if (!result.contains(obj)) {
                        result.addElement(obj);
                    }
                }
            }

            if (vectB != null) {
                for (int i = 0; i < vectB.size(); i++) {
                    Object obj = vectB.elementAt(i);

                    if (!result.contains(obj)) {
                        result.addElement(obj);
                    }
                }
            }

            return result;
        } else {
            // If we have a big enough set of elements to union, use a
            // temporary hashtable so that we have better scalability for
            // item lookup.

            Hashtable workSet = new Hashtable();
            Vector result = new Vector(threshold);
            Enumeration Enum;
            Object item;

            /* -- */

            if (vectA != null) {
                Enum = vectA.elements();

                while (Enum.hasMoreElements()) {
                    item = Enum.nextElement();
                    workSet.put(item, item);
                }
            }

            if (vectB != null) {
                Enum = vectB.elements();

                while (Enum.hasMoreElements()) {
                    item = Enum.nextElement();
                    workSet.put(item, item);
                }
            }

            Enum = workSet.elements();

            while (Enum.hasMoreElements()) {
                result.addElement(Enum.nextElement());
            }

            // we're big enough to be over threshold, so lets go ahead and
            // take the time to trim to size

            result.trimToSize();

            return result;
        }
    }
}

Related

  1. mergeVectors(Vector v1, Vector v2)
  2. union(Vector a, Vector b)
  3. unionAdd(Vector vect, Object obj)