jaroslav@1258: /* jaroslav@1258: * Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved. jaroslav@1258: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@1258: * jaroslav@1258: * This code is free software; you can redistribute it and/or modify it jaroslav@1258: * under the terms of the GNU General Public License version 2 only, as jaroslav@1258: * published by the Free Software Foundation. Oracle designates this jaroslav@1258: * particular file as subject to the "Classpath" exception as provided jaroslav@1258: * by Oracle in the LICENSE file that accompanied this code. jaroslav@1258: * jaroslav@1258: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@1258: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@1258: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@1258: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@1258: * accompanied this code). jaroslav@1258: * jaroslav@1258: * You should have received a copy of the GNU General Public License version jaroslav@1258: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@1258: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@1258: * jaroslav@1258: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@1258: * or visit www.oracle.com if you need additional information or have any jaroslav@1258: * questions. jaroslav@1258: */ jaroslav@1258: jaroslav@1258: package java.util; jaroslav@1258: jaroslav@1258: /** jaroslav@1258: *

Hash table and linked list implementation of the Set interface, jaroslav@1258: * with predictable iteration order. This implementation differs from jaroslav@1258: * HashSet in that it maintains a doubly-linked list running through jaroslav@1258: * all of its entries. This linked list defines the iteration ordering, jaroslav@1258: * which is the order in which elements were inserted into the set jaroslav@1258: * (insertion-order). Note that insertion order is not affected jaroslav@1258: * if an element is re-inserted into the set. (An element e jaroslav@1258: * is reinserted into a set s if s.add(e) is invoked when jaroslav@1258: * s.contains(e) would return true immediately prior to jaroslav@1258: * the invocation.) jaroslav@1258: * jaroslav@1258: *

This implementation spares its clients from the unspecified, generally jaroslav@1258: * chaotic ordering provided by {@link HashSet}, without incurring the jaroslav@1258: * increased cost associated with {@link TreeSet}. It can be used to jaroslav@1258: * produce a copy of a set that has the same order as the original, regardless jaroslav@1258: * of the original set's implementation: jaroslav@1258: *

jaroslav@1258:  *     void foo(Set s) {
jaroslav@1258:  *         Set copy = new LinkedHashSet(s);
jaroslav@1258:  *         ...
jaroslav@1258:  *     }
jaroslav@1258:  * 
jaroslav@1258: * This technique is particularly useful if a module takes a set on input, jaroslav@1258: * copies it, and later returns results whose order is determined by that of jaroslav@1258: * the copy. (Clients generally appreciate having things returned in the same jaroslav@1258: * order they were presented.) jaroslav@1258: * jaroslav@1258: *

This class provides all of the optional Set operations, and jaroslav@1258: * permits null elements. Like HashSet, it provides constant-time jaroslav@1258: * performance for the basic operations (add, contains and jaroslav@1258: * remove), assuming the hash function disperses elements jaroslav@1258: * properly among the buckets. Performance is likely to be just slightly jaroslav@1258: * below that of HashSet, due to the added expense of maintaining the jaroslav@1258: * linked list, with one exception: Iteration over a LinkedHashSet jaroslav@1258: * requires time proportional to the size of the set, regardless of jaroslav@1258: * its capacity. Iteration over a HashSet is likely to be more jaroslav@1258: * expensive, requiring time proportional to its capacity. jaroslav@1258: * jaroslav@1258: *

A linked hash set has two parameters that affect its performance: jaroslav@1258: * initial capacity and load factor. They are defined precisely jaroslav@1258: * as for HashSet. Note, however, that the penalty for choosing an jaroslav@1258: * excessively high value for initial capacity is less severe for this class jaroslav@1258: * than for HashSet, as iteration times for this class are unaffected jaroslav@1258: * by capacity. jaroslav@1258: * jaroslav@1258: *

Note that this implementation is not synchronized. jaroslav@1258: * If multiple threads access a linked hash set concurrently, and at least jaroslav@1258: * one of the threads modifies the set, it must be synchronized jaroslav@1258: * externally. This is typically accomplished by synchronizing on some jaroslav@1258: * object that naturally encapsulates the set. jaroslav@1258: * jaroslav@1258: * If no such object exists, the set should be "wrapped" using the jaroslav@1258: * {@link Collections#synchronizedSet Collections.synchronizedSet} jaroslav@1258: * method. This is best done at creation time, to prevent accidental jaroslav@1258: * unsynchronized access to the set:

jaroslav@1258:  *   Set s = Collections.synchronizedSet(new LinkedHashSet(...));
jaroslav@1258: * jaroslav@1258: *

The iterators returned by this class's iterator method are jaroslav@1258: * fail-fast: if the set is modified at any time after the iterator jaroslav@1258: * is created, in any way except through the iterator's own remove jaroslav@1258: * method, the iterator will throw a {@link ConcurrentModificationException}. jaroslav@1258: * Thus, in the face of concurrent modification, the iterator fails quickly jaroslav@1258: * and cleanly, rather than risking arbitrary, non-deterministic behavior at jaroslav@1258: * an undetermined time in the future. jaroslav@1258: * jaroslav@1258: *

Note that the fail-fast behavior of an iterator cannot be guaranteed jaroslav@1258: * as it is, generally speaking, impossible to make any hard guarantees in the jaroslav@1258: * presence of unsynchronized concurrent modification. Fail-fast iterators jaroslav@1258: * throw ConcurrentModificationException on a best-effort basis. jaroslav@1258: * Therefore, it would be wrong to write a program that depended on this jaroslav@1258: * exception for its correctness: the fail-fast behavior of iterators jaroslav@1258: * should be used only to detect bugs. jaroslav@1258: * jaroslav@1258: *

This class is a member of the jaroslav@1258: * jaroslav@1258: * Java Collections Framework. jaroslav@1258: * jaroslav@1258: * @param the type of elements maintained by this set jaroslav@1258: * jaroslav@1258: * @author Josh Bloch jaroslav@1258: * @see Object#hashCode() jaroslav@1258: * @see Collection jaroslav@1258: * @see Set jaroslav@1258: * @see HashSet jaroslav@1258: * @see TreeSet jaroslav@1258: * @see Hashtable jaroslav@1258: * @since 1.4 jaroslav@1258: */ jaroslav@1258: jaroslav@1258: public class LinkedHashSet jaroslav@1258: extends HashSet jaroslav@1258: implements Set, Cloneable, java.io.Serializable { jaroslav@1258: jaroslav@1258: private static final long serialVersionUID = -2851667679971038690L; jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Constructs a new, empty linked hash set with the specified initial jaroslav@1258: * capacity and load factor. jaroslav@1258: * jaroslav@1258: * @param initialCapacity the initial capacity of the linked hash set jaroslav@1258: * @param loadFactor the load factor of the linked hash set jaroslav@1258: * @throws IllegalArgumentException if the initial capacity is less jaroslav@1258: * than zero, or if the load factor is nonpositive jaroslav@1258: */ jaroslav@1258: public LinkedHashSet(int initialCapacity, float loadFactor) { jaroslav@1258: super(initialCapacity, loadFactor, true); jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Constructs a new, empty linked hash set with the specified initial jaroslav@1258: * capacity and the default load factor (0.75). jaroslav@1258: * jaroslav@1258: * @param initialCapacity the initial capacity of the LinkedHashSet jaroslav@1258: * @throws IllegalArgumentException if the initial capacity is less jaroslav@1258: * than zero jaroslav@1258: */ jaroslav@1258: public LinkedHashSet(int initialCapacity) { jaroslav@1258: super(initialCapacity, .75f, true); jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Constructs a new, empty linked hash set with the default initial jaroslav@1258: * capacity (16) and load factor (0.75). jaroslav@1258: */ jaroslav@1258: public LinkedHashSet() { jaroslav@1258: super(16, .75f, true); jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Constructs a new linked hash set with the same elements as the jaroslav@1258: * specified collection. The linked hash set is created with an initial jaroslav@1258: * capacity sufficient to hold the elements in the specified collection jaroslav@1258: * and the default load factor (0.75). jaroslav@1258: * jaroslav@1258: * @param c the collection whose elements are to be placed into jaroslav@1258: * this set jaroslav@1258: * @throws NullPointerException if the specified collection is null jaroslav@1258: */ jaroslav@1258: public LinkedHashSet(Collection c) { jaroslav@1258: super(Math.max(2*c.size(), 11), .75f, true); jaroslav@1258: addAll(c); jaroslav@1258: } jaroslav@1258: }