Source Code Cross Referenced for LinkedHashSet.java in  » 6.0-JDK-Core » Collections-Jar-Zip-Logging-regex » java » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » Collections Jar Zip Logging regex » java.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.