Java Vector Compare isEquals(Object[] array, Vector vector)

Here you can find the source of isEquals(Object[] array, Vector vector)

Description

is Equals

License

Open Source License

Parameter

Parameter Description
array an Object[]
vector a Vector

Return

true if the two arguments have the same elements (don't need to be in same order)

Declaration

public static boolean isEquals(Object[] array, Vector vector) 

Method Source Code

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

public class Main {
    /**/*  w w  w  .  j av  a 2s  . co m*/
     * @return true if the two arguments have the same elements (don't need to be in same order)
     * @param array an Object[]
     * @param vector a Vector 
     */
    public static boolean isEquals(Object[] array, Vector vector) {
        return isEquals(createVector(array), vector);
    }

    /**
     * @return true if the two arguments have the same elements (don't need to be in same order)
     * @param array an Object[]
     * @param vector a Vector 
     */
    public static boolean isEquals(Object[] array, Enumeration e) {
        return isEquals(createVector(array).elements(), e);
    }

    /**
     * @return true if the 2 Enumeration have the same Objects (could be in different order)
     * @param enum1 the first Enumeration
     * @param enum2 the second Enumeration
     */
    public static boolean isEquals(Enumeration enum1, Enumeration enum2) {
        if (enum1 == null || enum2 == null)
            return false;

        return isEquals(createVector(enum1), createVector(enum2));
    }

    /**
     * @return true if the 2 Vector have the same Objects (could be in different order)
     * @param v1 the first Vector
     * @param v2 the second Vector
     */
    public static boolean isEquals(Vector v1, Vector v2) {
        if (v1 == null || v2 == null)
            return false;

        Vector v1Clone = (Vector) v1.clone();
        Vector v2Clone = (Vector) v2.clone();

        if (v1Clone.size() != v2Clone.size())
            return false;

        for (int i = 0; i < v1Clone.size(); ++i) {
            Object v1CloneObj = v1Clone.elementAt(i);

            boolean found = false;
            Object v2CloneObj = null;

            for (int j = 0; j < v2Clone.size(); ++j) {
                v2CloneObj = v2Clone.elementAt(j);

                if (v1CloneObj.equals(v2CloneObj)) {
                    found = true;
                    break;
                }
            }

            if (found)
                v2Clone.removeElement(v2CloneObj);
            else
                return false;
        }

        return (v2Clone.size() == 0 ? true : false);
    }

    /**
     * @return true if the 2 List have the same Objects (could be in different order)
     * @param l1 the first List
     * @param l2 the second List
     */
    public static boolean isEquals(List l1, List l2) {
        return isEquals(createVector(l1), createVector(l2));
    }

    /**
     * @return true if the 2 Iterator have the same Objects (could be in different order)
     * @param i1 the first Iterator
     * @param i2 the second Iterator
     */
    public static boolean isEquals(Iterator i1, Iterator i2) {
        return isEquals(createVector(i1), createVector(i2));
    }

    /**
     * @return true if the Enumeration and Iterator are equals
     * @param enumeration the Enumeration
     * @param iterator the Iterator
     */
    public static boolean isEquals(Enumeration enumeration,
            Iterator iterator) {
        if (enumeration == null)
            return false;
        if (iterator == null)
            return false;

        return isEquals(enumeration, createVector(iterator).elements());
    }

    /**
     * @return true if the 2 Hashtable passed are equals
     * @param table1 the first Hashtable
     * @param table2 the second Hashtable
     */
    public static boolean isEquals(Hashtable table1, Hashtable table2) {
        if (table1.size() != table2.size())
            return false;

        Enumeration keys1 = table1.keys();

        while (keys1.hasMoreElements()) {
            Object key1 = keys1.nextElement();
            Object value1 = table1.get(key1);

            if (!table2.containsKey(key1))
                return false;

            if (table2.get(key1).equals(value1) == false)
                return false;
        }

        return true;
    }

    /**
     * @return true if the Hashtable and HashMap passed are equals
     * @param table the Hashtable
     * @param map the HashMap
     */
    public static boolean isEquals(Hashtable table, HashMap map) {
        return isEquals(table, createHashtable(map));
    }

    /**
     * @return a Vector with contents of the Iterator
     * @param iterator the Iterator object
     */
    public static Vector createVector(Iterator iterator) {
        Vector v = new Vector();

        while (iterator.hasNext())
            v.addElement(iterator.next());

        return v;
    }

    /**
     * @return a Vector with contents of the List
     * @param iterator the Iterator object
     */
    public static Vector createVector(List list) {
        return createVector(list.iterator());
    }

    /**
     * @return a Vector with contents of the Enumeration 
     * @param e the Enumeration object
     */
    public static Vector createVector(Enumeration e) {
        Vector v = new Vector();

        while (e.hasMoreElements())
            v.addElement(e.nextElement());

        return v;
    }

    /**
     * @return a Vector with contents of the Object[] 
     * @param array an Object[]
     */
    public static Vector createVector(Object[] array) {
        Vector v = new Vector();

        for (int i = 0; i < array.length; ++i)
            v.addElement(array[i]);

        return v;
    }

    /**
     * @return a Hashtable from the HashMap passed
     * @param map the HashMap
     */
    public static Hashtable createHashtable(HashMap map) {
        Hashtable table = new Hashtable();

        Iterator keys = map.keySet().iterator();

        while (keys.hasNext()) {
            Object key = keys.next();
            table.put(key, map.get(key));
        }

        return table;
    }
}

Related

  1. equals(Vector vFirst_, Vector vSecond_)
  2. equalVectors(Vector aV1, Vector aV2)
  3. isIdentical(Object[] array, Vector vector)