001: /*
002: * @(#)LinkedHashSet.java 1.8 03/01/20
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package java.util;
028:
029: /**
030: * <p>Hash table and linked list implementation of the <tt>Set</tt> interface,
031: * with predictable iteration order. This implementation differs from
032: * <tt>HashSet</tt> in that it maintains a doubly-linked list running through
033: * all of its entries. This linked list defines the iteration ordering,
034: * which is the order in which elements were inserted into the set
035: * (<i>insertion-order</i>). Note that insertion order is <i>not</i> affected
036: * if an element is <i>re-inserted</i> into the set. (An element <tt>e</tt>
037: * is reinserted into a set <tt>s</tt> if <tt>s.add(e)</tt> is invoked when
038: * <tt>s.contains(e)</tt> would return <tt>true</tt> immediately prior to
039: * the invocation.)
040: *
041: * <p>This implementation spares its clients from the unspecified, generally
042: * chaotic ordering provided by {@link HashSet}, without incurring the
043: * increased cost associated with {@link TreeSet}. It can be used to
044: * produce a copy of a set that has the same order as the original, regardless
045: * of the original set's implementation:
046: * <pre>
047: * void foo(Set m) {
048: * Set copy = new LinkedHashSet(m);
049: * ...
050: * }
051: * </pre>
052: * This technique is particularly useful if a module takes a set on input,
053: * copies it, and later returns results whose order is determined by that of
054: * the copy. (Clients generally appreciate having things returned in the same
055: * order they were presented.)
056: *
057: * <p>This class provides all of the optional <tt>Set</tt> operations, and
058: * permits null elements. Like <tt>HashSet</tt>, it provides constant-time
059: * performance for the basic operations (<tt>add</tt>, <tt>contains</tt> and
060: * <tt>remove</tt>), assuming the the hash function disperses elements
061: * properly among the buckets. Performance is likely to be just slightly
062: * below that of <tt>HashSet</tt>, due to the added expense of maintaining the
063: * linked list, with one exception: Iteration over a <tt>LinkedHashSet</tt>
064: * requires time proportional to the <i>size</i> of the set, regardless of
065: * its capacity. Iteration over a <tt>HashSet</tt> is likely to be more
066: * expensive, requiring time proportional to its <i>capacity</i>.
067: *
068: * <p>A linked hash set has two parameters that affect its performance:
069: * <i>initial capacity</i> and <i>load factor</i>. They are defined precisely
070: * as for <tt>HashSet</tt>. Note, however, that the penalty for choosing an
071: * excessively high value for initial capacity is less severe for this class
072: * than for <tt>HashSet</tt>, as iteration times for this class are unaffected
073: * by capacity.
074: *
075: * <p><strong>Note that this implementation is not synchronized.</strong> If
076: * multiple threads access a linked hash set concurrently, and at least one of
077: * the threads modifies the set, it <em>must</em> be synchronized externally.
078: * This is typically accomplished by synchronizing on some object that
079: * naturally encapsulates the set. If no such object exists, the set should
080: * be "wrapped" using the <tt>Collections.synchronizedSet</tt>method. This is
081: * best done at creation time, to prevent accidental unsynchronized access:
082: * <pre>
083: * Set s = Collections.synchronizedSet(new LinkedHashSet(...));
084: * </pre>
085: *
086: * <p>The iterators returned by the this class's <tt>iterator</tt> method are
087: * <em>fail-fast</em>: if the set is modified at any time after the iterator
088: * is created, in any way except through the iterator's own remove method, the
089: * iterator will throw a <tt>ConcurrentModificationException</tt>. Thus, in
090: * the face of concurrent modification, the Iterator fails quickly and
091: * cleanly, rather than risking arbitrary, non-deterministic behavior at an
092: * undetermined time in the future.
093: *
094: * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
095: * as it is, generally speaking, impossible to make any hard guarantees in the
096: * presence of unsynchronized concurrent modification. Fail-fast iterators
097: * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
098: * Therefore, it would be wrong to write a program that depended on this
099: * exception for its correctness: <i>the fail-fast behavior of iterators
100: * should be used only to detect bugs.</i>
101: *
102: * <p>This class is a member of the
103: * <a href="{@docRoot}/../guide/collections/index.html">
104: * Java Collections Framework</a>.
105: *
106: * @author Josh Bloch
107: * @version 1.8 01/20/03
108: * @see Object#hashCode()
109: * @see Collection
110: * @see Set
111: * @see HashSet
112: * @see TreeSet
113: * @see Hashtable
114: * @since 1.4
115: */
116:
117: public class LinkedHashSet extends HashSet implements Set, Cloneable,
118: java.io.Serializable {
119: /**
120: * Constructs a new, empty linked hash set with the specified initial
121: * capacity and load factor.
122: *
123: * @param initialCapacity the initial capacity of the linked hash set
124: * @param loadFactor the load factor of the linked hash set.
125: * @throws IllegalArgumentException if the initial capacity is less
126: * than zero, or if the load factor is nonpositive.
127: */
128: public LinkedHashSet(int initialCapacity, float loadFactor) {
129: super (initialCapacity, loadFactor, true);
130: }
131:
132: /**
133: * Constructs a new, empty linked hash set with the specified initial
134: * capacity and the default load factor (0.75).
135: *
136: * @param initialCapacity the initial capacity of the LinkedHashSet.
137: * @throws IllegalArgumentException if the initial capacity is less
138: * than zero.
139: */
140: public LinkedHashSet(int initialCapacity) {
141: super (initialCapacity, .75f, true);
142: }
143:
144: /**
145: * Constructs a new, empty linked hash set with the default initial
146: * capacity (16) and load factor (0.75).
147: */
148: public LinkedHashSet() {
149: super (16, .75f, true);
150: }
151:
152: /**
153: * Constructs a new linked hash set with the same elements as the
154: * specified collection. The linked hash set is created with an initial
155: * capacity sufficient to hold the elements in the specified collection
156: * and the default load factor (0.75).
157: *
158: * @param c the collection whose elements are to be placed into
159: * this set.
160: * @throws NullPointerException if the specified collection is null.
161: */
162: public LinkedHashSet(Collection c) {
163: super (Math.max(2 * c.size(), 11), .75f, true);
164: addAll(c);
165: }
166: }
|