📄 hashset.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN""http://www.w3.org/TR/REC-html40/frameset.dtd"><!--NewPage--><HTML><HEAD><!-- Generated by javadoc on Thu Apr 27 23:36:55 PDT 2000 --><TITLE>Java 2 Platform SE v1.3: Class HashSet</TITLE><LINK REL ="stylesheet" TYPE="text/css" HREF="../../stylesheet.css" TITLE="Style"></HEAD><BODY BGCOLOR="white"><!-- ========== START OF NAVBAR ========== --><A NAME="navbar_top"><!-- --></A><TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0"><TR><TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1"><A NAME="navbar_top_firstrow"><!-- --></A><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3"> <TR ALIGN="center" VALIGN="top"> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../overview-summary.html"><FONT CLASS="NavBarFont1"><B>Overview</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-summary.html"><FONT CLASS="NavBarFont1"><B>Package</B></FONT></A> </TD> <TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> <FONT CLASS="NavBarFont1Rev"><B>Class</B></FONT> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="class-use/HashSet.html"><FONT CLASS="NavBarFont1"><B>Use</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../index-files/index-1.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A> </TD> </TR></TABLE></TD><TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM><b>Java<sup><font size=-2>TM</font></sup> 2 Platform<br>Std. Ed. v1.3</b></EM></TD></TR><TR><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2"> <A HREF="../../java/util/HashMap.html"><B>PREV CLASS</B></A> <A HREF="../../java/util/Hashtable.html"><B>NEXT CLASS</B></A></FONT></TD><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2"> <A HREF="../../index.html" TARGET="_top"><B>FRAMES</B></A> <A HREF="HashSet.html" TARGET="_top"><B>NO FRAMES</B></A></FONT></TD></TR><TR><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2"> SUMMARY: INNER | FIELD | <A HREF="#constructor_summary">CONSTR</A> | <A HREF="#method_summary">METHOD</A></FONT></TD><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">DETAIL: FIELD | <A HREF="#constructor_detail">CONSTR</A> | <A HREF="#method_detail">METHOD</A></FONT></TD></TR></TABLE><!-- =========== END OF NAVBAR =========== --><HR><!-- ======== START OF CLASS DATA ======== --><H2><FONT SIZE="-1">java.util</FONT><BR>Class HashSet</H2><PRE><A HREF="../../java/lang/Object.html">java.lang.Object</A> | +--<A HREF="../../java/util/AbstractCollection.html">java.util.AbstractCollection</A> | +--<A HREF="../../java/util/AbstractSet.html">java.util.AbstractSet</A> | +--<B>java.util.HashSet</B></PRE><DL><DT><B>All Implemented Interfaces:</B> <DD><A HREF="../../java/lang/Cloneable.html">Cloneable</A>, <A HREF="../../java/util/Collection.html">Collection</A>, <A HREF="../../java/io/Serializable.html">Serializable</A>, <A HREF="../../java/util/Set.html">Set</A></DD></DL><HR><DL><DT>public class <B>HashSet</B><DT>extends <A HREF="../../java/util/AbstractSet.html">AbstractSet</A><DT>implements <A HREF="../../java/util/Set.html">Set</A>, <A HREF="../../java/lang/Cloneable.html">Cloneable</A>, <A HREF="../../java/io/Serializable.html">Serializable</A></DL><P>This class implements the <tt>Set</tt> interface, backed by a hash table (actually a <tt>HashMap</tt> instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the <tt>null</tt> element.<p> This class offers constant time performance for the basic operations (<tt>add</tt>, <tt>remove</tt>, <tt>contains</tt> and <tt>size</tt>), assuming the hash function disperses the elements properly among the buckets. Iterating over this set requires time proportional to the sum of the <tt>HashSet</tt> instance's size (the number of elements) plus the "capacity" of the backing <tt>HashMap</tt> instance (the number of buckets). Thus, it's very important not to set the intial capacity too high (or the load factor too low) if iteration performance is important.<p> <b>Note that this implementation is not synchronized.</b> If multiple threads access a set concurrently, and at least one of the threads modifies the set, it <i>must</i> be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the set. If no such object exists, the set should be "wrapped" using the <tt>Collections.synchronizedSet</tt> method. This is best done at creation time, to prevent accidental unsynchronized access to the <tt>HashSet</tt> instance: <pre> Set s = Collections.synchronizedSet(new HashSet(...)); </pre><p> The iterators returned by this class's <tt>iterator</tt> method are <i>fail-fast</i>: if the set is modified at any time after the iterator is created, in any way except through the iterator's own <tt>remove</tt> method, the Iterator throws a <tt>ConcurrentModificationException</tt>. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.<P><DL><DT><B>Since: </B><DD>1.2</DD><DT><B>See Also: </B><DD><A HREF="../../java/util/Collection.html"><CODE>Collection</CODE></A>, <A HREF="../../java/util/Set.html"><CODE>Set</CODE></A>, <A HREF="../../java/util/TreeSet.html"><CODE>TreeSet</CODE></A>, <A HREF="../../java/util/Collections.html#synchronizedSet(java.util.Set)"><CODE>Collections.synchronizedSet(Set)</CODE></A>, <A HREF="../../java/util/HashMap.html"><CODE>HashMap</CODE></A>, <A HREF="../../serialized-form.html#java.util.HashSet">Serialized Form</A></DL><HR><P><!-- ======== INNER CLASS SUMMARY ======== --><!-- =========== FIELD SUMMARY =========== --><!-- ======== CONSTRUCTOR SUMMARY ======== --><A NAME="constructor_summary"><!-- --></A><TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%"><TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor"><TD COLSPAN=2><FONT SIZE="+2"><B>Constructor Summary</B></FONT></TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD><CODE><B><A HREF="../../java/util/HashSet.html#HashSet()">HashSet</A></B>()</CODE><BR> Constructs a new, empty set; the backing <tt>HashMap</tt> instance has default capacity and load factor, which is <tt>0.75</tt>.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD><CODE><B><A HREF="../../java/util/HashSet.html#HashSet(java.util.Collection)">HashSet</A></B>(<A HREF="../../java/util/Collection.html">Collection</A> c)</CODE><BR> Constructs a new set containing the elements in the specified collection.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD><CODE><B><A HREF="../../java/util/HashSet.html#HashSet(int)">HashSet</A></B>(int initialCapacity)</CODE><BR> Constructs a new, empty set; the backing <tt>HashMap</tt> instance has the specified initial capacity and default load factor, which is <tt>0.75</tt>.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD><CODE><B><A HREF="../../java/util/HashSet.html#HashSet(int, float)">HashSet</A></B>(int initialCapacity, float loadFactor)</CODE><BR> Constructs a new, empty set; the backing <tt>HashMap</tt> instance has the specified initial capacity and the specified load factor.</TD></TR></TABLE> <!-- ========== METHOD SUMMARY =========== --><A NAME="method_summary"><!-- --></A><TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%"><TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor"><TD COLSPAN=2><FONT SIZE="+2"><B>Method Summary</B></FONT></TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE> boolean</CODE></FONT></TD><TD><CODE><B><A HREF="../../java/util/HashSet.html#add(java.lang.Object)">add</A></B>(<A HREF="../../java/lang/Object.html">Object</A> o)</CODE><BR> Adds the specified element to this set if it is not already present.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE> void</CODE></FONT></TD><TD><CODE><B><A HREF="../../java/util/HashSet.html#clear()">clear</A></B>()</CODE><BR> Removes all of the elements from this set.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE> <A HREF="../../java/lang/Object.html">Object</A></CODE></FONT></TD><TD><CODE><B><A HREF="../../java/util/HashSet.html#clone()">clone</A></B>()</CODE><BR> Returns a shallow copy of this <tt>HashSet</tt> instance: the elements themselves are not cloned.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE> boolean</CODE></FONT></TD><TD><CODE><B><A HREF="../../java/util/HashSet.html#contains(java.lang.Object)">contains</A></B>(<A HREF="../../java/lang/Object.html">Object</A> o)</CODE><BR> Returns <tt>true</tt> if this set contains the specified element.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE> boolean</CODE></FONT></TD><TD><CODE><B><A HREF="../../java/util/HashSet.html#isEmpty()">isEmpty</A></B>()</CODE><BR> Returns <tt>true</tt> if this set contains no elements.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE> <A HREF="../../java/util/Iterator.html">Iterator</A></CODE></FONT></TD><TD><CODE><B><A HREF="../../java/util/HashSet.html#iterator()">iterator</A></B>()</CODE><BR> Returns an iterator over the elements in this set.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE> boolean</CODE></FONT></TD><TD><CODE><B><A HREF="../../java/util/HashSet.html#remove(java.lang.Object)">remove</A></B>(<A HREF="../../java/lang/Object.html">Object</A> o)</CODE><BR> Removes the given element from this set if it is present.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE> int</CODE></FONT></TD><TD><CODE><B><A HREF="../../java/util/HashSet.html#size()">size</A></B>()</CODE><BR> Returns the number of elements in this set (its cardinality).</TD></TR></TABLE> <A NAME="methods_inherited_from_class_java.util.AbstractSet"><!-- --></A><TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%"><TR BGCOLOR="#EEEEFF" CLASS="TableSubHeadingColor"><TD><B>Methods inherited from class java.util.<A HREF="../../java/util/AbstractSet.html">AbstractSet</A></B></TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD><CODE><A HREF="../../java/util/AbstractSet.html#equals(java.lang.Object)">equals</A>, <A HREF="../../java/util/AbstractSet.html#hashCode()">hashCode</A>, <A HREF="../../java/util/AbstractSet.html#removeAll(java.util.Collection)">removeAll</A></CODE></TD></TR></TABLE> <A NAME="methods_inherited_from_class_java.util.AbstractCollection"><!-- --></A><TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%"><TR BGCOLOR="#EEEEFF" CLASS="TableSubHeadingColor"><TD><B>Methods inherited from class java.util.<A HREF="../../java/util/AbstractCollection.html">AbstractCollection</A></B></TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD><CODE><A HREF="../../java/util/AbstractCollection.html#addAll(java.util.Collection)">addAll</A>, <A HREF="../../java/util/AbstractCollection.html#containsAll(java.util.Collection)">containsAll</A>, <A HREF="../../java/util/AbstractCollection.html#retainAll(java.util.Collection)">retainAll</A>, <A HREF="../../java/util/AbstractCollection.html#toArray()">toArray</A>, <A HREF="../../java/util/AbstractCollection.html#toArray(java.lang.Object[])">toArray</A>, <A HREF="../../java/util/AbstractCollection.html#toString()">toString</A></CODE></TD></TR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -