Java Vector Union mergeVectors(Vector v1, Vector v2)

Here you can find the source of mergeVectors(Vector v1, Vector v2)

Description

Return a vector with entries existing in both vectors

License

Open Source License

Parameter

Parameter Description
v1 Vector1
v2 Vector2

Return

Vector or null if vector v1 or v2 not passed

Declaration

public static Vector mergeVectors(Vector v1, Vector v2) 

Method Source Code

//package com.java2s;
/* IBS Copyright/Security Notice ***************************************
 *                                              
 *  BAP Property of IBS AB//from  w  ww  .java2s.  co  m
 *  (C) Copyright IBS AB 2001
 *  All rights reserved.                        
 *  Use, duplication, or disclosure restricted  
 *  by license agreement with IBS AB.           
 *                                              
 *  Licensed Materials - Property of IBS AB     
 *                                              
 * End IBS Copyright/Security Notice **********************************/

import java.util.*;

public class Main {
    /**
     * Return a vector with entries existing in both vectors
     *
     * @param   v1      Vector1 
     * @param   v2      Vector2 
     * @return   Vector or null if vector v1 or v2 not passed
     */
    public static Vector mergeVectors(Vector v1, Vector v2) {

        if (v1 == null || v2 == null) {
            return null;
        }

        Vector v = new Vector();
        if (v1.isEmpty() || v2.isEmpty()) {
            return v;
        }

        int size = v1.size();
        Object obj = null;
        for (int i = 0; i < size; i++) {
            obj = v1.get(i);
            if (v2.contains(obj)) {
                v.add(obj);
            }
        }

        return v;
    }
}

Related

  1. union(Vector a, Vector b)
  2. union(Vector vectA, Vector vectB)
  3. unionAdd(Vector vect, Object obj)