Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import java.util.Hashtable;

import java.util.Vector;

public class Main {
    /**
     * Method getHashtableFromVector.
     * @param vector Vector
     * @return Hashtable
     */
    public static Hashtable getHashtableFromVector(Vector vector) {
        Hashtable hashtable = new Hashtable(vector.size());

        for (int i = 0; i < vector.size(); i++) {
            hashtable.put(vector.elementAt(i), vector.elementAt(i));
        }

        return hashtable;
    }

    /**
     * Method getHashtableFromVector.
     * @param vector Vector
     * @param keyGetter String
     * @return Hashtable
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public static Hashtable getHashtableFromVector(Vector vector, String keyGetter)
            throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Hashtable hashtable = new Hashtable(vector.size());

        Object vectorElement = null;
        Object keyObject = null;
        Method keyGetterMethod = null;
        Class[] clsParms = new Class[0];
        Object[] objParms = new Object[0];

        for (int i = 0; i < vector.size(); i++) {
            vectorElement = vector.elementAt(i);
            keyGetterMethod = vectorElement.getClass().getMethod(keyGetter, clsParms);
            keyObject = keyGetterMethod.invoke(vectorElement, objParms);

            hashtable.put(keyObject, vectorElement);
        }

        return hashtable;
    }
}