1   /*
2    * %W% %E%
3    *
4    * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
5    * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6    */
7   
8   package java.util;
9   
10  /**
11   * <p>Hash table and linked list implementation of the <tt>Set</tt> interface,
12   * with predictable iteration order.  This implementation differs from
13   * <tt>HashSet</tt> in that it maintains a doubly-linked list running through
14   * all of its entries.  This linked list defines the iteration ordering,
15   * which is the order in which elements were inserted into the set
16   * (<i>insertion-order</i>).  Note that insertion order is <i>not</i> affected
17   * if an element is <i>re-inserted</i> into the set.  (An element <tt>e</tt>
18   * is reinserted into a set <tt>s</tt> if <tt>s.add(e)</tt> is invoked when
19   * <tt>s.contains(e)</tt> would return <tt>true</tt> immediately prior to
20   * the invocation.)
21   *
22   * <p>This implementation spares its clients from the unspecified, generally
23   * chaotic ordering provided by {@link HashSet}, without incurring the
24   * increased cost associated with {@link TreeSet}.  It can be used to
25   * produce a copy of a set that has the same order as the original, regardless
26   * of the original set's implementation:
27   * <pre>
28   *     void foo(Set s) {
29   *         Set copy = new LinkedHashSet(s);
30   *         ...
31   *     }
32   * </pre>
33   * This technique is particularly useful if a module takes a set on input,
34   * copies it, and later returns results whose order is determined by that of
35   * the copy.  (Clients generally appreciate having things returned in the same
36   * order they were presented.)
37   *
38   * <p>This class provides all of the optional <tt>Set</tt> operations, and
39   * permits null elements.  Like <tt>HashSet</tt>, it provides constant-time
40   * performance for the basic operations (<tt>add</tt>, <tt>contains</tt> and
41   * <tt>remove</tt>), assuming the hash function disperses elements
42   * properly among the buckets.  Performance is likely to be just slightly
43   * below that of <tt>HashSet</tt>, due to the added expense of maintaining the
44   * linked list, with one exception: Iteration over a <tt>LinkedHashSet</tt>
45   * requires time proportional to the <i>size</i> of the set, regardless of
46   * its capacity.  Iteration over a <tt>HashSet</tt> is likely to be more
47   * expensive, requiring time proportional to its <i>capacity</i>.
48   *
49   * <p>A linked hash set has two parameters that affect its performance:
50   * <i>initial capacity</i> and <i>load factor</i>.  They are defined precisely
51   * as for <tt>HashSet</tt>.  Note, however, that the penalty for choosing an
52   * excessively high value for initial capacity is less severe for this class
53   * than for <tt>HashSet</tt>, as iteration times for this class are unaffected
54   * by capacity.
55   *
56   * <p><strong>Note that this implementation is not synchronized.</strong>
57   * If multiple threads access a linked hash set concurrently, and at least
58   * one of the threads modifies the set, it <em>must</em> be synchronized
59   * externally.  This is typically accomplished by synchronizing on some
60   * object that naturally encapsulates the set.
61   *
62   * If no such object exists, the set should be "wrapped" using the
63   * {@link Collections#synchronizedSet Collections.synchronizedSet}
64   * method.  This is best done at creation time, to prevent accidental
65   * unsynchronized access to the set: <pre>
66   *   Set s = Collections.synchronizedSet(new LinkedHashSet(...));</pre>
67   *
68   * <p>The iterators returned by this class's <tt>iterator</tt> method are
69   * <em>fail-fast</em>: if the set is modified at any time after the iterator
70   * is created, in any way except through the iterator's own <tt>remove</tt>
71   * method, the iterator will throw a {@link ConcurrentModificationException}.
72   * Thus, in the face of concurrent modification, the iterator fails quickly
73   * and cleanly, rather than risking arbitrary, non-deterministic behavior at
74   * an undetermined time in the future.
75   *
76   * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
77   * as it is, generally speaking, impossible to make any hard guarantees in the
78   * presence of unsynchronized concurrent modification.  Fail-fast iterators
79   * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
80   * Therefore, it would be wrong to write a program that depended on this
81   * exception for its correctness:   <i>the fail-fast behavior of iterators
82   * should be used only to detect bugs.</i>
83   *
84   * <p>This class is a member of the
85   * <a href="{@docRoot}/../technotes/guides/collections/index.html">
86   * Java Collections Framework</a>.
87   *
88   * @param <E> the type of elements maintained by this set
89   *
90   * @author  Josh Bloch
91   * @version %I% %E%
92   * @see     Object#hashCode()
93   * @see     Collection
94   * @see     Set
95   * @see     HashSet
96   * @see     TreeSet
97   * @see     Hashtable
98   * @since   1.4
99   */
100 
101 public class LinkedHashSet<E>
102     extends HashSet<E>
103     implements Set<E>, Cloneable, java.io.Serializable {
104 
105     private static final long serialVersionUID = -2851667679971038690L;
106 
107     /**
108      * Constructs a new, empty linked hash set with the specified initial
109      * capacity and load factor.
110      *
111      * @param      initialCapacity the initial capacity of the linked hash set
112      * @param      loadFactor      the load factor of the linked hash set
113      * @throws     IllegalArgumentException  if the initial capacity is less
114      *               than zero, or if the load factor is nonpositive
115      */
116     public LinkedHashSet(int initialCapacity, float loadFactor) {
117         super(initialCapacity, loadFactor, true);
118     }
119 
120     /**
121      * Constructs a new, empty linked hash set with the specified initial
122      * capacity and the default load factor (0.75).
123      *
124      * @param   initialCapacity   the initial capacity of the LinkedHashSet
125      * @throws  IllegalArgumentException if the initial capacity is less
126      *              than zero
127      */
128     public LinkedHashSet(int initialCapacity) {
129         super(initialCapacity, .75f, true);
130     }
131 
132     /**
133      * Constructs a new, empty linked hash set with the default initial
134      * capacity (16) and load factor (0.75).
135      */
136     public LinkedHashSet() {
137         super(16, .75f, true);
138     }
139 
140     /**
141      * Constructs a new linked hash set with the same elements as the
142      * specified collection.  The linked hash set is created with an initial
143      * capacity sufficient to hold the elements in the specified collection
144      * and the default load factor (0.75).
145      *
146      * @param c  the collection whose elements are to be placed into
147      *           this set
148      * @throws NullPointerException if the specified collection is null
149      */
150     public LinkedHashSet(Collection<? extends E> c) {
151         super(Math.max(2*c.size(), 11), .75f, true);
152         addAll(c);
153     }
154 }
155