📄 javautil.doc4.html
字号:
<html>
<head>
<title>The Java Language Specification The Package java.util</title>
</head>
<body BGCOLOR=#eeeeff text=#000000 LINK=#0000ff VLINK=#000077 ALINK=#ff0000>
<a href="index.html">Contents</a> | <a href="javautil.doc3.html">Prev</a> | <a href="javautil.doc5.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<hr><br>
<a name="23193"></a>
<center><h1>21.5 The Class <code>java.util.Hashtable</code></h1></center>
<a name="23197"></a>
The class <code>Hashtable</code> implements the abstract class <code>Dictionary</code> <a href="javautil.doc3.html#7498">(§21.4)</a>, with
some additional functionality.
<p><pre><a name="23198"></a>public class <code><b>Hashtable</b></code> extends Dictionary implements Cloneable {
<a name="23199"></a> public <code><b>Hashtable</b></code>(int initialCapacity, float loadFactor);
<a name="23200"></a> public <code><b>Hashtable</b></code>(int initialCapacity);
<a name="23201"></a> public <code><b>Hashtable</b></code>();
<a name="23202"></a> public String <code><b>toString</b></code>();
<a name="23203"></a> public Object <code><b>clone</b></code>();
<a name="23204"></a> public int <code><b>size</b></code>();
<a name="23205"></a> public boolean <code><b>isEmpty</b></code>();
<a name="23206"></a> public Object <code><b>get</b></code>(Object key)
<a name="23207"></a> throws NullPointerException;
<a name="23208"></a> public Object <code><b>put</b></code>(Object key, Object value)
<a name="23209"></a> throws NullPointerException;
<a name="23210"></a> public Object <code><b>remove</b></code>(Object key)
<a name="23211"></a> throws NullPointerException;
<a name="23212"></a> public Enumeration <code><b>keys</b></code>();
<a name="23213"></a> public Enumeration <code><b>elements</b></code>();
<a name="23214"></a> public boolean <code><b>contains</b></code>(Object value);
<a name="23215"></a> public boolean <code><b>containsKey</b></code>(Object key);
<a name="23216"></a> protected void <code><b>rehash</b></code>();
<a name="23217"></a> public void <code><b>clear</b></code>();
<a name="23218"></a>}
</pre><a name="23219"></a>
A <code>Hashtable</code> has two parameters that affect its efficiency: its <i>capacity</i> and its <i>load factor</i>. The load factor should be between <code>0.0</code> and <code>1.0</code>. When the number of entries in the hashtable exceeds the product of the load factor and the current capacity, the capacity is increased, using the <code>rehash</code> method. Larger load factors use memory more efficiently at the expense of larger expected time per lookup. If many entries are to be made in a <code>Hashtable</code>, creating it with a sufficiently large capacity may allow the entries to be inserted more efficiently than letting it perform automatic rehashing as needed to grow the table.<p>
<a name="23220"></a>
<p><font size=+1><strong>21.5.1 </strong> <code>public <code><b>Hashtable</b></code>(int initialCapacity, float loadFactor)</code></font>
<p>
<a name="23221"></a>
This constructor initializes a newly created <code>Hashtable</code> object so that its capacity
is <code>initialCapacity</code> and its load factor is <code>loadFactor</code>. Initially, there are no
entries in the table.
<p><a name="23222"></a>
<p><font size=+1><strong>21.5.2 </strong> <code>public <code><b>Hashtable</b></code>(int initialCapacity)</code></font>
<p>
<a name="23223"></a>
This constructor initializes a newly created <code>Hashtable</code> object so that its capacity
is <code>initialCapacity</code> and its load factor is <code>0.75</code>. Initially, there are no entries in
the table.
<p><a name="23224"></a>
<p><font size=+1><strong>21.5.3 </strong> <code>public <code><b>Hashtable</b></code>()</code></font>
<p>
<a name="23225"></a>
This constructor initializes a newly created <code>Hashtable</code> object so that its load factor
is <code>0.75</code>. Initially, there are no entries in the table.
<p><a name="23226"></a>
<p><font size=+1><strong>21.5.4 </strong> <code>public String <code><b>toString</b></code>()</code></font>
<p>
<a name="23227"></a>
This <code>Hashtable</code> is represented in string form as a set of entries, enclosed in
braces and separated by the ASCII characters "<code>, </code>" (comma and space). Each
entry is rendered as the key, an equals sign <code>=</code>, and the associated element, where
the <code>toString</code> method is used to convert the key and element to strings.
<p><a name="23231"></a>
Overrides the <code>toString</code> method of <code>Object</code> <a href="javautil.doc1.html#8597">(§21.2.3)</a>.<p>
<a name="23232"></a>
<p><font size=+1><strong>21.5.5 </strong> <code>public Object <code><b>clone</b></code>()</code></font>
<p>
<a name="23233"></a>
A copy of this <code>Hashtable</code> is constructed and returned. All the structure of the
hashtable itself is copied, but the keys and elements are not cloned.
<p><a name="23237"></a>
Overrides the <code>clone</code> method of <code>Object</code> <a href="javautil.doc1.html#8602">(§21.2.6)</a>.<p>
<a name="23238"></a>
<p><font size=+1><strong>21.5.6 </strong> <code>public int <code><b>size</b></code>()</code></font>
<p>
<a name="23242"></a>
Implements the <code>size</code> method of <code>Dictionary</code> <a href="javautil.doc3.html#7508">(§21.4.1)</a>.
<p><a name="23243"></a>
<p><font size=+1><strong>21.5.7 </strong> <code>public boolean <code><b>isEmpty</b></code>()</code></font>
<p>
<a name="23247"></a>
Implements the <code>isEmpty</code> method of <code>Dictionary</code> <a href="javautil.doc3.html#7509">(§21.4.2)</a>.
<p><a name="23248"></a>
<p><font size=+1><strong>21.5.8 </strong> <code>public Object <code><b>get</b></code>(Object key)</code></font>
<p>
<a name="23252"></a>
Implements the <code>get</code> method of <code>Dictionary</code> <a href="javautil.doc3.html#7512">(§21.4.3)</a>.
<p><a name="23253"></a>
<p><font size=+1><strong>21.5.9 </strong> <code>public Object <code><b>put</b></code>(Object key, Object value)</code></font>
<p>
<a name="23257"></a>
Implements the <code>put</code> method of <code>Dictionary</code> <a href="javautil.doc3.html#7513">(§21.4.4)</a>.
<p><a name="23258"></a>
<p><font size=+1><strong>21.5.10 </strong> <code>public Object <code><b>remove</b></code>(Object key)</code></font>
<p>
<a name="23262"></a>
Implements the <code>remove</code> method of <code>Dictionary</code> <a href="javautil.doc3.html#7514">(§21.4.5)</a>.
<p><a name="23263"></a>
<p><font size=+1><strong>21.5.11 </strong> <code>public Enumeration <code><b>keys</b></code>()</code></font>
<p>
<a name="23267"></a>
Implements the <code>keys</code> method of <code>Dictionary</code> <a href="javautil.doc3.html#21235">(§21.4.6)</a>.
<p><a name="23268"></a>
<p><font size=+1><strong>21.5.12 </strong> <code>public Enumeration <code><b>elements</b></code>()</code></font>
<p>
<a name="23272"></a>
Implements the <code>elements</code> method of <code>Dictionary</code> <a href="javautil.doc3.html#21236">(§21.4.7)</a>.
<p><a name="23273"></a>
<p><font size=+1><strong>21.5.13 </strong> <code>public boolean <code><b>contains</b></code>(Object value)</code></font>
<p>
<a name="23274"></a>
The result is <code>true</code> if and only if this <code>Hashtable</code> contains at least one entry for
which the element is equal to <code>value</code>, as determined by the <code>equals</code> method
<a href="javalang.doc1.html#14865">(§20.1.3)</a>.
<p><a name="23278"></a>
<p><font size=+1><strong>21.5.14 </strong> <code>public boolean <code><b>containsKey</b></code>(Object key)</code></font>
<p>
<a name="23279"></a>
The result is <code>true</code> if and only if this <code>Hashtable</code> contains an entry for which the
key is equal to <code>key</code>, as determined by the <code>equals</code> method <a href="javalang.doc1.html#14865">(§20.1.3)</a>. In other
words, this method produces the same result as the expression:
<p><pre><a name="23283"></a>get(key) != null
</pre><a name="24866"></a>
<p><font size=+1><strong>21.5.15 </strong> <code>protected void <code><b>rehash</b></code>()</code></font>
<p>
<a name="24867"></a>
This <code>Hashtable</code> is increased in capacity and reorganized internally, in order to
accommodate and access its entries more efficiently.
<p><a name="24872"></a>
<p><font size=+1><strong>21.5.16 </strong> <code>public void <code><b>clear</b></code>()</code></font>
<p>
<a name="23287"></a>
The <code>clear</code> method removes all entries from this <code>Hashtable</code>.<img src="javautil.doc.anc12.gif">
<p>
<hr>
<!-- This inserts footnotes--><p>
<a href="index.html">Contents</a> | <a href="javautil.doc3.html">Prev</a> | <a href="javautil.doc5.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<p>
<font size=-1>Java Language Specification (HTML generated by Suzette Pelouch on February 24, 1998)<br>
<i><a href="jcopyright.doc.html">Copyright © 1996 Sun Microsystems, Inc.</a>
All rights reserved</i>
<br>
Please send any comments or corrections to <a href="mailto:doug.kramer@sun.com">doug.kramer@sun.com</a>
</font>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -