1 *java.util.Hashtable* *Hashtable* This class implements a hashtable, which maps
3 public class Hashtable<K,V>
4 extends |java.util.Dictionary|
5 implements |java.util.Map|
9 |java.util.Hashtable_Description|
10 |java.util.Hashtable_Fields|
11 |java.util.Hashtable_Constructors|
12 |java.util.Hashtable_Methods|
14 ================================================================================
16 *java.util.Hashtable_Constructors*
17 |java.util.Hashtable()|Constructs a new, empty hashtable with a default initial
18 |java.util.Hashtable(int)|Constructs a new, empty hashtable with the specified
19 |java.util.Hashtable(int,float)|Constructs a new, empty hashtable with the spec
20 |java.util.Hashtable(Map<?extendsK,?extendsV>)|Constructs a new hashtable with
22 *java.util.Hashtable_Methods*
23 |java.util.Hashtable.clear()|Clears this hashtable so that it contains no keys.
24 |java.util.Hashtable.clone()|Creates a shallow copy of this hashtable.
25 |java.util.Hashtable.contains(Object)|Tests if some key maps into the specified
26 |java.util.Hashtable.containsKey(Object)|Tests if the specified object is a key
27 |java.util.Hashtable.containsValue(Object)|Returns true if this hashtable maps
28 |java.util.Hashtable.elements()|Returns an enumeration of the values in this ha
29 |java.util.Hashtable.entrySet()|Returns aSetview of the mappings contained in t
30 |java.util.Hashtable.equals(Object)|Compares the specified Object with this Map
31 |java.util.Hashtable.get(Object)|Returns the value to which the specified key i
32 |java.util.Hashtable.hashCode()|Returns the hash code value for this Map as per
33 |java.util.Hashtable.isEmpty()|Tests if this hashtable maps no keys to values.
34 |java.util.Hashtable.keys()|Returns an enumeration of the keys in this hashtabl
35 |java.util.Hashtable.keySet()|Returns aSetview of the keys contained in this ma
36 |java.util.Hashtable.put(K,V)|Maps the specified key to the specified value in
37 |java.util.Hashtable.putAll(Map<?extendsK,?extendsV>)|Copies all of the mapping
38 |java.util.Hashtable.rehash()|Increases the capacity of and internally reorgani
39 |java.util.Hashtable.remove(Object)|Removes the key (and its corresponding valu
40 |java.util.Hashtable.size()|Returns the number of keys in this hashtable.
41 |java.util.Hashtable.toString()|Returns a string representation of this Hashtab
42 |java.util.Hashtable.values()|Returns aCollectionview of the values contained i
44 *java.util.Hashtable_Description*
46 This class implements a hashtable, which maps keys to values. Any non-null
47 object can be used as a key or as a value.
49 To successfully store and retrieve objects from a hashtable, the objects used
50 as keys must implement the hashCode method and the equals method.
52 An instance of Hashtable has two parameters that affect its performance:
53 initial capacity and load factor. The capacity is the number of buckets in the
54 hash table, and the initial capacity is simply the capacity at the time the
55 hash table is created. Note that the hash table is open: in the case of a "hash
56 collision", a single bucket stores multiple entries, which must be searched
57 sequentially. The load factor is a measure of how full the hash table is
58 allowed to get before its capacity is automatically increased. The initial
59 capacity and load factor parameters are merely hints to the implementation. The
60 exact details as to when and whether the rehash method is invoked are
61 implementation-dependent.
63 Generally, the default load factor (.75) offers a good tradeoff between time
64 and space costs. Higher values decrease the space overhead but increase the
65 time cost to look up an entry (which is reflected in most Hashtable operations,
66 including get and put).
68 The initial capacity controls a tradeoff between wasted space and the need for
69 rehash operations, which are time-consuming. No rehash operations will ever
70 occur if the initial capacity is greater than the maximum number of entries the
71 Hashtable will contain divided by its load factor. However, setting the initial
72 capacity too high can waste space.
74 If many entries are to be made into a Hashtable, creating it with a
75 sufficiently large capacity may allow the entries to be inserted more
76 efficiently than letting it perform automatic rehashing as needed to grow the
79 This example creates a hashtable of numbers. It uses the names of the numbers
82 Hashtable numbers = new Hashtable(); numbers.put("one", 1); numbers.put("two",
83 2); numbers.put("three", 3);
85 To retrieve a number, use the following code:
87 Integer n = numbers.get("two"); if (n != null) { System.out.println("two = " +
90 The iterators returned by the iterator method of the collections returned by
91 all of this class's "collection view methods" are fail-fast: if the Hashtable
92 is structurally modified at any time after the iterator is created, in any way
93 except through the iterator's own remove method, the iterator will throw a
94 (|java.util.ConcurrentModificationException|) . Thus, in the face of concurrent
95 modification, the iterator fails quickly and cleanly, rather than risking
96 arbitrary, non-deterministic behavior at an undetermined time in the future.
97 The Enumerations returned by Hashtable's keys and elements methods are not
100 Note that the fail-fast behavior of an iterator cannot be guaranteed as it is,
101 generally speaking, impossible to make any hard guarantees in the presence of
102 unsynchronized concurrent modification. Fail-fast iterators throw
103 ConcurrentModificationException on a best-effort basis. Therefore, it would be
104 wrong to write a program that depended on this exception for its correctness:
105 the fail-fast behavior of iterators should be used only to detect bugs.
107 As of the Java 2 platform v1.2, this class was retrofitted to implement the
108 (|java.util.Map|) interface, making it a member of the <a
109 href="/../technotes/guides/collections/index.html"> Java Collections Framework.
110 Unlike the new collection implementations,Hashtableis synchronized.
114 *java.util.Hashtable()*
118 Constructs a new, empty hashtable with a default initial capacity (11) and load
122 *java.util.Hashtable(int)*
124 public Hashtable(int initialCapacity)
126 Constructs a new, empty hashtable with the specified initial capacity and
127 default load factor (0.75).
129 initialCapacity - the initial capacity of the hashtable.
131 *java.util.Hashtable(int,float)*
137 Constructs a new, empty hashtable with the specified initial capacity and the
138 specified load factor.
140 initialCapacity - the initial capacity of the hashtable.
141 loadFactor - the load factor of the hashtable.
143 *java.util.Hashtable(Map<?extendsK,?extendsV>)*
145 public Hashtable(java.util.Map<? extends K, ? extends V> t)
147 Constructs a new hashtable with the same mappings as the given Map. The
148 hashtable is created with an initial capacity sufficient to hold the mappings
149 in the given Map and a default load factor (0.75).
151 t - the map whose mappings are to be placed in this map.
153 *java.util.Hashtable.clear()*
155 public synchronized void clear()
157 Clears this hashtable so that it contains no keys.
161 *java.util.Hashtable.clone()*
163 public synchronized |java.lang.Object| clone()
165 Creates a shallow copy of this hashtable. All the structure of the hashtable
166 itself is copied, but the keys and values are not cloned. This is a relatively
171 Returns: a clone of the hashtable
173 *java.util.Hashtable.contains(Object)*
175 public synchronized boolean contains(java.lang.Object value)
177 Tests if some key maps into the specified value in this hashtable. This
178 operation is more expensive than the containsKey(|java.util.Hashtable|) method.
180 Note that this method is identical in functionality to
181 containsValue(|java.util.Hashtable|) , (which is part of the (|java.util.Map|)
182 interface in the collections framework).
185 value - a value to search for
187 Returns: true if and only if some key maps to the value argument in this hashtable as
188 determined by the equals method; false otherwise.
190 *java.util.Hashtable.containsKey(Object)*
192 public synchronized boolean containsKey(java.lang.Object key)
194 Tests if the specified object is a key in this hashtable.
199 Returns: true if and only if the specified object is a key in this hashtable, as
200 determined by the equals method; false otherwise.
202 *java.util.Hashtable.containsValue(Object)*
204 public boolean containsValue(java.lang.Object value)
206 Returns true if this hashtable maps one or more keys to this value.
208 Note that this method is identical in functionality to
209 contains(|java.util.Hashtable|) (which predates the (|java.util.Map|)
213 value - value whose presence in this hashtable is to be tested
215 Returns: true if this map maps one or more keys to the specified value
217 *java.util.Hashtable.elements()*
219 public synchronized |java.util.Enumeration|<V> elements()
221 Returns an enumeration of the values in this hashtable. Use the Enumeration
222 methods on the returned object to fetch the elements sequentially.
226 Returns: an enumeration of the values in this hashtable.
228 *java.util.Hashtable.entrySet()*
230 public |java.util.Set|<Entry<K,V>> entrySet()
232 Returns a (|java.util.Set|) view of the mappings contained in this map. The set
233 is backed by the map, so changes to the map are reflected in the set, and
234 vice-versa. If the map is modified while an iteration over the set is in
235 progress (except through the iterator's own remove operation, or through the
236 setValue operation on a map entry returned by the iterator) the results of the
237 iteration are undefined. The set supports element removal, which removes the
238 corresponding mapping from the map, via the Iterator.remove, Set.remove,
239 removeAll, retainAll and clear operations. It does not support the add or
244 *java.util.Hashtable.equals(Object)*
246 public synchronized boolean equals(java.lang.Object o)
248 Compares the specified Object with this Map for equality, as per the definition
249 in the Map interface.
252 o - object to be compared for equality with this hashtable
254 Returns: true if the specified Object is equal to this Map
256 *java.util.Hashtable.get(Object)*
258 public synchronized |V| get(java.lang.Object key)
260 Returns the value to which the specified key is mapped, ornullif this map
261 contains no mapping for the key.
263 More formally, if this map contains a mapping from a keykto a valuevsuch
264 that(key.equals(k)), then this method returnsv; otherwise it returnsnull.
265 (There can be at most one such mapping.)
268 key - the key whose associated value is to be returned
270 Returns: the value to which the specified key is mapped, or {@code null} if this map
271 contains no mapping for the key
273 *java.util.Hashtable.hashCode()*
275 public synchronized int hashCode()
277 Returns the hash code value for this Map as per the definition in the Map
282 *java.util.Hashtable.isEmpty()*
284 public synchronized boolean isEmpty()
286 Tests if this hashtable maps no keys to values.
290 Returns: true if this hashtable maps no keys to values; false otherwise.
292 *java.util.Hashtable.keys()*
294 public synchronized |java.util.Enumeration|<K> keys()
296 Returns an enumeration of the keys in this hashtable.
300 Returns: an enumeration of the keys in this hashtable.
302 *java.util.Hashtable.keySet()*
304 public |java.util.Set|<K> keySet()
306 Returns a (|java.util.Set|) view of the keys contained in this map. The set is
307 backed by the map, so changes to the map are reflected in the set, and
308 vice-versa. If the map is modified while an iteration over the set is in
309 progress (except through the iterator's own remove operation), the results of
310 the iteration are undefined. The set supports element removal, which removes
311 the corresponding mapping from the map, via the Iterator.remove, Set.remove,
312 removeAll, retainAll, and clear operations. It does not support the add or
317 *java.util.Hashtable.put(K,V)*
319 public synchronized |V| put(
323 Maps the specified key to the specified value in this hashtable. Neither the
324 key nor the value can be null.
326 The value can be retrieved by calling the get method with a key that is equal
330 key - the hashtable key
333 Returns: the previous value of the specified key in this hashtable, or null if it did
336 *java.util.Hashtable.putAll(Map<?extendsK,?extendsV>)*
338 public synchronized void putAll(java.util.Map<? extends K, ? extends V> t)
340 Copies all of the mappings from the specified map to this hashtable. These
341 mappings will replace any mappings that this hashtable had for any of the keys
342 currently in the specified map.
345 t - mappings to be stored in this map
347 *java.util.Hashtable.rehash()*
349 protected void rehash()
351 Increases the capacity of and internally reorganizes this hashtable, in order
352 to accommodate and access its entries more efficiently. This method is called
353 automatically when the number of keys in the hashtable exceeds this hashtable's
354 capacity and load factor.
358 *java.util.Hashtable.remove(Object)*
360 public synchronized |V| remove(java.lang.Object key)
362 Removes the key (and its corresponding value) from this hashtable. This method
363 does nothing if the key is not in the hashtable.
366 key - the key that needs to be removed
368 Returns: the value to which the key had been mapped in this hashtable, or null if the
369 key did not have a mapping
371 *java.util.Hashtable.size()*
373 public synchronized int size()
375 Returns the number of keys in this hashtable.
379 Returns: the number of keys in this hashtable.
381 *java.util.Hashtable.toString()*
383 public synchronized |java.lang.String| toString()
385 Returns a string representation of this Hashtable object in the form of a set
386 of entries, enclosed in braces and separated by the ASCII characters "," (comma
387 and space). Each entry is rendered as the key, an equals sign =, and the
388 associated element, where the toString method is used to convert the key and
393 Returns: a string representation of this hashtable
395 *java.util.Hashtable.values()*
397 public |java.util.Collection|<V> values()
399 Returns a (|java.util.Collection|) view of the values contained in this map.
400 The collection is backed by the map, so changes to the map are reflected in the
401 collection, and vice-versa. If the map is modified while an iteration over the
402 collection is in progress (except through the iterator's own remove operation),
403 the results of the iteration are undefined. The collection supports element
404 removal, which removes the corresponding mapping from the map, via the
405 Iterator.remove, Collection.remove, removeAll, retainAll and clear operations.
406 It does not support the add or addAll operations.