其实在Java2之前,Java是没有完整的集合框架的。它只有一些简单的可以自扩展的容器类,比如Vector,Stack,Hashtable等。
java.util.Hashtable与Java2中的Map类似,可以看成一种关联或映射数组,可以将两个或多个毫无关系的对象相关联。
它的基本目标是实现两个或多个对象之间进行关联。
举一个现实生活中的例子,比如我们说美国白宫时,指的就是在美国华盛顿的总统办公大楼, 为什么一说到美国白宫,总统办公大楼呢?这是我们人为的对“美国白宫”和总统办公大楼进行了关联,
本来“美国白宫”就是四个普通的文字,现在却有了不同的含义。
在Java中我们就可以用String定义一个内容为“美国白宫”的对象变量,
在定义一个总统大楼的对象变量,把它们进行关联,这就是Hashtable的用意。
通过使用pub(Object key,Object value)方法把两个对象进行关联,需要时用get(Object key)取得与key关联的值对象。
还可以查询某个对象的索引值等等。值得说明的这里的get方法查找一个对象时与Vector中的get方法在内部实现时有很大不同,
在一个Hashtable中查找一个键对象要比在一个Vector中快的多。这是因为Hashtable使用了一种哈希表的技术(在数据结构中有详细讲解),
在Java每个对象缺省都有一个通过Object的hashCode()方法获得的哈希码,Hashtable就是利用这个哈希实现快速查找键对象的。
注意1 :Hashtable is a synchronized implementation of Map.
它是线程安全的,它的很多方法都是synchronized的,它也是通过锁(自身的)来实现线程安全,其效率也很低
注意2 :其大小也是动态增长的。
注意3 :Neither keys nor values can be null. (Use HashMap or LinkedHashMap if you need null keys or values.)
注意4 :如果对一个key连续放入多个value.那么key所对应的value是最后放入的那个。
它的基本目标是实现两个或多个对象之间进行关联。
举一个现实生活中的例子,比如我们说美国白宫时,指的就是在美国华盛顿的总统办公大楼, 为什么一说到美国白宫,总统办公大楼呢?这是我们人为的对“美国白宫”和总统办公大楼进行了关联,
本来“美国白宫”就是四个普通的文字,现在却有了不同的含义。
在Java中我们就可以用String定义一个内容为“美国白宫”的对象变量,
在定义一个总统大楼的对象变量,把它们进行关联,这就是Hashtable的用意。
通过使用pub(Object key,Object value)方法把两个对象进行关联,需要时用get(Object key)取得与key关联的值对象。
还可以查询某个对象的索引值等等。值得说明的这里的get方法查找一个对象时与Vector中的get方法在内部实现时有很大不同,
在一个Hashtable中查找一个键对象要比在一个Vector中快的多。这是因为Hashtable使用了一种哈希表的技术(在数据结构中有详细讲解),
在Java每个对象缺省都有一个通过Object的hashCode()方法获得的哈希码,Hashtable就是利用这个哈希实现快速查找键对象的。
它是线程安全的,它的很多方法都是synchronized的,它也是通过锁(自身的)来实现线程安全,其效率也很低
构造函数
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Hashtable()
Constructs a new
Hashtable using the default capacity and load factor.
| |||||||||||
Hashtable(int capacity)
Constructs a new
Hashtable using the specified capacity and the default load factor.
| |||||||||||
Hashtable(int capacity, float loadFactor)
Constructs a new
Hashtable using the specified capacity and load factor.
| |||||||||||
Hashtable(
Map<? extends K, ? extends V> map)
Constructs a new instance of
Hashtable containing the mappings from the specified map.
|
主要函数
synchronized void |
clear()
Removes all key/value pairs from this
Hashtable , leaving the size zero and the capacity unchanged.
|
synchronized Object |
clone()
Returns a new
Hashtable with the same key/value pairs, capacity and load factor.
|
boolean |
contains(
Object value)
Returns true if this
Hashtable contains the specified object as the value of at least one of the key/value pairs.
|
synchronized boolean |
containsKey(
Object key)
Returns true if this
Hashtable contains the specified object as a key of one of the key/value pairs.
|
synchronized boolean |
containsValue(
Object value)
Searches this
Hashtable for the specified value.
|
synchronized Enumeration<V> |
elements()
Returns an enumeration on the values of this
Hashtable .
|
synchronized Set< Entry<K, V>> |
entrySet()
Returns a set of the mappings contained in this
Hashtable .
|
synchronized boolean |
equals(
Object object)
Compares this
Hashtable with the specified object and indicates if they are equal.
|
synchronized V |
get(
Object key)
Returns the value associated with the specified key in this
Hashtable .
|
synchronized int |
hashCode()
Returns an integer hash code for this object.
|
synchronized boolean |
isEmpty()
Returns true if this
Hashtable has no key/value pairs.
|
synchronized Set<K> |
keySet()
Returns a set of the keys contained in this
Hashtable .
|
synchronized Enumeration<K> |
keys()
Returns an enumeration on the keys of this
Hashtable instance.
|
synchronized V |
put(K key, V value)
Associate the specified value with the specified key in this
Hashtable .
|
synchronized void |
putAll(
Map<? extends K, ? extends V> map)
Copies every mapping to this
Hashtable from the specified map.
|
synchronized V |
remove(
Object key)
Removes the key/value pair with the specified key from this
Hashtable .
|
synchronized int |
size()
Returns the number of key/value pairs in this
Hashtable .
|
synchronized String |
toString()
Returns the string representation of this
Hashtable .
|
synchronized Collection<V> |
values()
Returns a collection of the values contained in this
Hashtable .
|
示例1:
Hashtable <String ,String> table=new Hashtable();
table.put(key, value);
table.put(key, value2);
System.out.println("value:"+table.get(key));
Hashtable <String ,String> table=new Hashtable();
table.put(key, value);
table.put(key, value2);
System.out.println("value:"+table.get(key));