convert Hash Table To Hash Map - Java java.util

Java examples for java.util:HashMap

Description

convert Hash Table To Hash Map

Demo Code


//package com.java2s;

import java.util.HashMap;

import java.util.Hashtable;
import java.util.Iterator;

import java.util.Map;

public class Main {

    public static HashMap converHashTableToHashMap(Hashtable data) {
        if (data == null) {
            return null;
        }/*ww w.  j  a  v  a  2  s. co m*/
        HashMap retHashMap = new HashMap();
        Iterator iter = data.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            String key = String.valueOf(entry.getKey());
            String value = String.valueOf(entry.getValue());
            retHashMap.put(key, value);
        }
        return retHashMap;
    }

    public static String getValue(Hashtable hash, String key,
            String defaultValue) {
        if (hash == null) {
            return defaultValue;
        }
        if (hash.containsKey(key)) {
            return String.valueOf(hash.get(key));
        } else {
            return defaultValue;
        }
    }
}

Related Tutorials