emul/compact/src/main/java/java/util/LinkedHashSet.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 07 Sep 2013 13:51:24 +0200
branchjdk7-b147
changeset 1258 724f3e1ea53e
permissions -rw-r--r--
Additional set of classes to make porting of lookup library more easier
jaroslav@1258
     1
/*
jaroslav@1258
     2
 * Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved.
jaroslav@1258
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@1258
     4
 *
jaroslav@1258
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@1258
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@1258
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@1258
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@1258
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@1258
    10
 *
jaroslav@1258
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@1258
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@1258
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@1258
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@1258
    15
 * accompanied this code).
jaroslav@1258
    16
 *
jaroslav@1258
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@1258
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@1258
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@1258
    20
 *
jaroslav@1258
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@1258
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@1258
    23
 * questions.
jaroslav@1258
    24
 */
jaroslav@1258
    25
jaroslav@1258
    26
package java.util;
jaroslav@1258
    27
jaroslav@1258
    28
/**
jaroslav@1258
    29
 * <p>Hash table and linked list implementation of the <tt>Set</tt> interface,
jaroslav@1258
    30
 * with predictable iteration order.  This implementation differs from
jaroslav@1258
    31
 * <tt>HashSet</tt> in that it maintains a doubly-linked list running through
jaroslav@1258
    32
 * all of its entries.  This linked list defines the iteration ordering,
jaroslav@1258
    33
 * which is the order in which elements were inserted into the set
jaroslav@1258
    34
 * (<i>insertion-order</i>).  Note that insertion order is <i>not</i> affected
jaroslav@1258
    35
 * if an element is <i>re-inserted</i> into the set.  (An element <tt>e</tt>
jaroslav@1258
    36
 * is reinserted into a set <tt>s</tt> if <tt>s.add(e)</tt> is invoked when
jaroslav@1258
    37
 * <tt>s.contains(e)</tt> would return <tt>true</tt> immediately prior to
jaroslav@1258
    38
 * the invocation.)
jaroslav@1258
    39
 *
jaroslav@1258
    40
 * <p>This implementation spares its clients from the unspecified, generally
jaroslav@1258
    41
 * chaotic ordering provided by {@link HashSet}, without incurring the
jaroslav@1258
    42
 * increased cost associated with {@link TreeSet}.  It can be used to
jaroslav@1258
    43
 * produce a copy of a set that has the same order as the original, regardless
jaroslav@1258
    44
 * of the original set's implementation:
jaroslav@1258
    45
 * <pre>
jaroslav@1258
    46
 *     void foo(Set s) {
jaroslav@1258
    47
 *         Set copy = new LinkedHashSet(s);
jaroslav@1258
    48
 *         ...
jaroslav@1258
    49
 *     }
jaroslav@1258
    50
 * </pre>
jaroslav@1258
    51
 * This technique is particularly useful if a module takes a set on input,
jaroslav@1258
    52
 * copies it, and later returns results whose order is determined by that of
jaroslav@1258
    53
 * the copy.  (Clients generally appreciate having things returned in the same
jaroslav@1258
    54
 * order they were presented.)
jaroslav@1258
    55
 *
jaroslav@1258
    56
 * <p>This class provides all of the optional <tt>Set</tt> operations, and
jaroslav@1258
    57
 * permits null elements.  Like <tt>HashSet</tt>, it provides constant-time
jaroslav@1258
    58
 * performance for the basic operations (<tt>add</tt>, <tt>contains</tt> and
jaroslav@1258
    59
 * <tt>remove</tt>), assuming the hash function disperses elements
jaroslav@1258
    60
 * properly among the buckets.  Performance is likely to be just slightly
jaroslav@1258
    61
 * below that of <tt>HashSet</tt>, due to the added expense of maintaining the
jaroslav@1258
    62
 * linked list, with one exception: Iteration over a <tt>LinkedHashSet</tt>
jaroslav@1258
    63
 * requires time proportional to the <i>size</i> of the set, regardless of
jaroslav@1258
    64
 * its capacity.  Iteration over a <tt>HashSet</tt> is likely to be more
jaroslav@1258
    65
 * expensive, requiring time proportional to its <i>capacity</i>.
jaroslav@1258
    66
 *
jaroslav@1258
    67
 * <p>A linked hash set has two parameters that affect its performance:
jaroslav@1258
    68
 * <i>initial capacity</i> and <i>load factor</i>.  They are defined precisely
jaroslav@1258
    69
 * as for <tt>HashSet</tt>.  Note, however, that the penalty for choosing an
jaroslav@1258
    70
 * excessively high value for initial capacity is less severe for this class
jaroslav@1258
    71
 * than for <tt>HashSet</tt>, as iteration times for this class are unaffected
jaroslav@1258
    72
 * by capacity.
jaroslav@1258
    73
 *
jaroslav@1258
    74
 * <p><strong>Note that this implementation is not synchronized.</strong>
jaroslav@1258
    75
 * If multiple threads access a linked hash set concurrently, and at least
jaroslav@1258
    76
 * one of the threads modifies the set, it <em>must</em> be synchronized
jaroslav@1258
    77
 * externally.  This is typically accomplished by synchronizing on some
jaroslav@1258
    78
 * object that naturally encapsulates the set.
jaroslav@1258
    79
 *
jaroslav@1258
    80
 * If no such object exists, the set should be "wrapped" using the
jaroslav@1258
    81
 * {@link Collections#synchronizedSet Collections.synchronizedSet}
jaroslav@1258
    82
 * method.  This is best done at creation time, to prevent accidental
jaroslav@1258
    83
 * unsynchronized access to the set: <pre>
jaroslav@1258
    84
 *   Set s = Collections.synchronizedSet(new LinkedHashSet(...));</pre>
jaroslav@1258
    85
 *
jaroslav@1258
    86
 * <p>The iterators returned by this class's <tt>iterator</tt> method are
jaroslav@1258
    87
 * <em>fail-fast</em>: if the set is modified at any time after the iterator
jaroslav@1258
    88
 * is created, in any way except through the iterator's own <tt>remove</tt>
jaroslav@1258
    89
 * method, the iterator will throw a {@link ConcurrentModificationException}.
jaroslav@1258
    90
 * Thus, in the face of concurrent modification, the iterator fails quickly
jaroslav@1258
    91
 * and cleanly, rather than risking arbitrary, non-deterministic behavior at
jaroslav@1258
    92
 * an undetermined time in the future.
jaroslav@1258
    93
 *
jaroslav@1258
    94
 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
jaroslav@1258
    95
 * as it is, generally speaking, impossible to make any hard guarantees in the
jaroslav@1258
    96
 * presence of unsynchronized concurrent modification.  Fail-fast iterators
jaroslav@1258
    97
 * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
jaroslav@1258
    98
 * Therefore, it would be wrong to write a program that depended on this
jaroslav@1258
    99
 * exception for its correctness:   <i>the fail-fast behavior of iterators
jaroslav@1258
   100
 * should be used only to detect bugs.</i>
jaroslav@1258
   101
 *
jaroslav@1258
   102
 * <p>This class is a member of the
jaroslav@1258
   103
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
jaroslav@1258
   104
 * Java Collections Framework</a>.
jaroslav@1258
   105
 *
jaroslav@1258
   106
 * @param <E> the type of elements maintained by this set
jaroslav@1258
   107
 *
jaroslav@1258
   108
 * @author  Josh Bloch
jaroslav@1258
   109
 * @see     Object#hashCode()
jaroslav@1258
   110
 * @see     Collection
jaroslav@1258
   111
 * @see     Set
jaroslav@1258
   112
 * @see     HashSet
jaroslav@1258
   113
 * @see     TreeSet
jaroslav@1258
   114
 * @see     Hashtable
jaroslav@1258
   115
 * @since   1.4
jaroslav@1258
   116
 */
jaroslav@1258
   117
jaroslav@1258
   118
public class LinkedHashSet<E>
jaroslav@1258
   119
    extends HashSet<E>
jaroslav@1258
   120
    implements Set<E>, Cloneable, java.io.Serializable {
jaroslav@1258
   121
jaroslav@1258
   122
    private static final long serialVersionUID = -2851667679971038690L;
jaroslav@1258
   123
jaroslav@1258
   124
    /**
jaroslav@1258
   125
     * Constructs a new, empty linked hash set with the specified initial
jaroslav@1258
   126
     * capacity and load factor.
jaroslav@1258
   127
     *
jaroslav@1258
   128
     * @param      initialCapacity the initial capacity of the linked hash set
jaroslav@1258
   129
     * @param      loadFactor      the load factor of the linked hash set
jaroslav@1258
   130
     * @throws     IllegalArgumentException  if the initial capacity is less
jaroslav@1258
   131
     *               than zero, or if the load factor is nonpositive
jaroslav@1258
   132
     */
jaroslav@1258
   133
    public LinkedHashSet(int initialCapacity, float loadFactor) {
jaroslav@1258
   134
        super(initialCapacity, loadFactor, true);
jaroslav@1258
   135
    }
jaroslav@1258
   136
jaroslav@1258
   137
    /**
jaroslav@1258
   138
     * Constructs a new, empty linked hash set with the specified initial
jaroslav@1258
   139
     * capacity and the default load factor (0.75).
jaroslav@1258
   140
     *
jaroslav@1258
   141
     * @param   initialCapacity   the initial capacity of the LinkedHashSet
jaroslav@1258
   142
     * @throws  IllegalArgumentException if the initial capacity is less
jaroslav@1258
   143
     *              than zero
jaroslav@1258
   144
     */
jaroslav@1258
   145
    public LinkedHashSet(int initialCapacity) {
jaroslav@1258
   146
        super(initialCapacity, .75f, true);
jaroslav@1258
   147
    }
jaroslav@1258
   148
jaroslav@1258
   149
    /**
jaroslav@1258
   150
     * Constructs a new, empty linked hash set with the default initial
jaroslav@1258
   151
     * capacity (16) and load factor (0.75).
jaroslav@1258
   152
     */
jaroslav@1258
   153
    public LinkedHashSet() {
jaroslav@1258
   154
        super(16, .75f, true);
jaroslav@1258
   155
    }
jaroslav@1258
   156
jaroslav@1258
   157
    /**
jaroslav@1258
   158
     * Constructs a new linked hash set with the same elements as the
jaroslav@1258
   159
     * specified collection.  The linked hash set is created with an initial
jaroslav@1258
   160
     * capacity sufficient to hold the elements in the specified collection
jaroslav@1258
   161
     * and the default load factor (0.75).
jaroslav@1258
   162
     *
jaroslav@1258
   163
     * @param c  the collection whose elements are to be placed into
jaroslav@1258
   164
     *           this set
jaroslav@1258
   165
     * @throws NullPointerException if the specified collection is null
jaroslav@1258
   166
     */
jaroslav@1258
   167
    public LinkedHashSet(Collection<? extends E> c) {
jaroslav@1258
   168
        super(Math.max(2*c.size(), 11), .75f, true);
jaroslav@1258
   169
        addAll(c);
jaroslav@1258
   170
    }
jaroslav@1258
   171
}