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