jdk:http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Hashtable.html
Hashtable numbers = new Hashtable(); numbers.put("one", new Integer(1)); numbers.put("two", new Integer(2)); numbers.put("three", new Integer(3));
To retrieve a number, use the following code:
Integer n = (Integer)numbers.get("two"); if (n != null) { System.out.println("two = " + n); }
Enumeration keys()
Returns an enumeration of the keys in this hashtable.
Set keySet()
Returns a Set view of the keys contained in this Hashtable.
Object put(Object key, Object value)
Maps the specified key to the specified value in this hashtable.
Collection values()
Returns a Collection view of the values contained in this Hashtable.
Object clone()
Creates a shallow copy of this hashtable.
boolean contains(Object value)
Tests if some key maps into the specified value in this hashtable.
boolean containsKey(Object key)
Tests if the specified object is a key in this hashtable.
boolean containsValue(Object value)
Returns true if this Hashtable maps one or more keys to this value.
contains containskey区别,没什么区别,只不过containskey是很早以前的方法,contains是collection采用的方法。
官方说法:
contains:Note that this method is identical in functionality to containsValue, (which is part of the Map interface in the collections framework).
遍历方法:
1.
private static void test() { Hashtable<String,String> table = new Hashtable<String,String>(); table.put("1", "hello"); table.put("2", "world!"); Iterator i = table.entrySet().iterator(); while(i.hasNext()){ Entry entry = (Entry) i.next(); System.out.println(entry.getKey()+"=>"+entry.getValue()); } }
table.entrySet返回的是一个set集合。集合里的元素为Entry。
第二种方法:
public class Hashtable1 { Hashtable<String,String> table=new Hashtable<String,String>(); public void retrieve() { table.put("1","hello"); table.put("2","world!"); Enumeration e=table.keys(); while(e.hasMoreElements()) { Object elem=e.nextElement(); System.out.println(elem+"=>"+table.get(elem)); } }
Enumeration遍历方法:
- public interface Enumeration
An object that implements the Enumeration interface generates a series of elements, one at a time. Successive calls to the nextElement
method return successive elements of the series.
For example, to print all elements of a vector v:
for (Enumeration e = v.elements() ; e.hasMoreElements() ;) { System.out.println(e.nextElement());
}
深入:
http://www.blogjava.net/fhtdy2004/archive/2009/07/03/285330.html
http://blog.csdn.net/teedry/article/details/4280034