mirror of
https://github.com/CyC2018/CS-Notes.git
synced 2025-07-19 01:30:41 +08:00
auto commit
This commit is contained in:
@ -1,37 +1,3 @@
|
||||
/*
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is available under and governed by the GNU General Public
|
||||
* License version 2 only, as published by the Free Software Foundation.
|
||||
* However, the following notice accompanied the original version of this
|
||||
* file:
|
||||
*
|
||||
* Written by Doug Lea with assistance from members of JCP JSR-166
|
||||
* Expert Group and released to the public domain, as explained at
|
||||
* http://creativecommons.org/publicdomain/zero/1.0/
|
||||
*/
|
||||
|
||||
package java.util.concurrent;
|
||||
import java.util.concurrent.locks.*;
|
||||
@ -41,64 +7,6 @@ import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
||||
/**
|
||||
* A hash table supporting full concurrency of retrievals and
|
||||
* adjustable expected concurrency for updates. This class obeys the
|
||||
* same functional specification as {@link java.util.Hashtable}, and
|
||||
* includes versions of methods corresponding to each method of
|
||||
* <tt>Hashtable</tt>. However, even though all operations are
|
||||
* thread-safe, retrieval operations do <em>not</em> entail locking,
|
||||
* and there is <em>not</em> any support for locking the entire table
|
||||
* in a way that prevents all access. This class is fully
|
||||
* interoperable with <tt>Hashtable</tt> in programs that rely on its
|
||||
* thread safety but not on its synchronization details.
|
||||
*
|
||||
* <p> Retrieval operations (including <tt>get</tt>) generally do not
|
||||
* block, so may overlap with update operations (including
|
||||
* <tt>put</tt> and <tt>remove</tt>). Retrievals reflect the results
|
||||
* of the most recently <em>completed</em> update operations holding
|
||||
* upon their onset. For aggregate operations such as <tt>putAll</tt>
|
||||
* and <tt>clear</tt>, concurrent retrievals may reflect insertion or
|
||||
* removal of only some entries. Similarly, Iterators and
|
||||
* Enumerations return elements reflecting the state of the hash table
|
||||
* at some point at or since the creation of the iterator/enumeration.
|
||||
* They do <em>not</em> throw {@link ConcurrentModificationException}.
|
||||
* However, iterators are designed to be used by only one thread at a time.
|
||||
*
|
||||
* <p> The allowed concurrency among update operations is guided by
|
||||
* the optional <tt>concurrencyLevel</tt> constructor argument
|
||||
* (default <tt>16</tt>), which is used as a hint for internal sizing. The
|
||||
* table is internally partitioned to try to permit the indicated
|
||||
* number of concurrent updates without contention. Because placement
|
||||
* in hash tables is essentially random, the actual concurrency will
|
||||
* vary. Ideally, you should choose a value to accommodate as many
|
||||
* threads as will ever concurrently modify the table. Using a
|
||||
* significantly higher value than you need can waste space and time,
|
||||
* and a significantly lower value can lead to thread contention. But
|
||||
* overestimates and underestimates within an order of magnitude do
|
||||
* not usually have much noticeable impact. A value of one is
|
||||
* appropriate when it is known that only one thread will modify and
|
||||
* all others will only read. Also, resizing this or any other kind of
|
||||
* hash table is a relatively slow operation, so, when possible, it is
|
||||
* a good idea to provide estimates of expected table sizes in
|
||||
* constructors.
|
||||
*
|
||||
* <p>This class and its views and iterators implement all of the
|
||||
* <em>optional</em> methods of the {@link Map} and {@link Iterator}
|
||||
* interfaces.
|
||||
*
|
||||
* <p> Like {@link Hashtable} but unlike {@link HashMap}, this class
|
||||
* does <em>not</em> allow <tt>null</tt> to be used as a key or value.
|
||||
*
|
||||
* <p>This class is a member of the
|
||||
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
|
||||
* Java Collections Framework</a>.
|
||||
*
|
||||
* @since 1.5
|
||||
* @author Doug Lea
|
||||
* @param <K> the type of keys maintained by this map
|
||||
* @param <V> the type of mapped values
|
||||
*/
|
||||
public class ConcurrentHashMap<K, V> extends AbstractMap<K, V>
|
||||
implements ConcurrentMap<K, V>, Serializable {
|
||||
private static final long serialVersionUID = 7249069246763182397L;
|
||||
|
@ -1,126 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package java.util;
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* Hash table based implementation of the <tt>Map</tt> interface. This
|
||||
* implementation provides all of the optional map operations, and permits
|
||||
* <tt>null</tt> values and the <tt>null</tt> key. (The <tt>HashMap</tt>
|
||||
* class is roughly equivalent to <tt>Hashtable</tt>, except that it is
|
||||
* unsynchronized and permits nulls.) This class makes no guarantees as to
|
||||
* the order of the map; in particular, it does not guarantee that the order
|
||||
* will remain constant over time.
|
||||
*
|
||||
* <p>This implementation provides constant-time performance for the basic
|
||||
* operations (<tt>get</tt> and <tt>put</tt>), assuming the hash function
|
||||
* disperses the elements properly among the buckets. Iteration over
|
||||
* collection views requires time proportional to the "capacity" of the
|
||||
* <tt>HashMap</tt> instance (the number of buckets) plus its size (the number
|
||||
* of key-value mappings). Thus, it's very important not to set the initial
|
||||
* capacity too high (or the load factor too low) if iteration performance is
|
||||
* important.
|
||||
*
|
||||
* <p>An instance of <tt>HashMap</tt> has two parameters that affect its
|
||||
* performance: <i>initial capacity</i> and <i>load factor</i>. The
|
||||
* <i>capacity</i> is the number of buckets in the hash table, and the initial
|
||||
* capacity is simply the capacity at the time the hash table is created. The
|
||||
* <i>load factor</i> is a measure of how full the hash table is allowed to
|
||||
* get before its capacity is automatically increased. When the number of
|
||||
* entries in the hash table exceeds the product of the load factor and the
|
||||
* current capacity, the hash table is <i>rehashed</i> (that is, internal data
|
||||
* structures are rebuilt) so that the hash table has approximately twice the
|
||||
* number of buckets.
|
||||
*
|
||||
* <p>As a general rule, the default load factor (.75) offers a good tradeoff
|
||||
* between time and space costs. Higher values decrease the space overhead
|
||||
* but increase the lookup cost (reflected in most of the operations of the
|
||||
* <tt>HashMap</tt> class, including <tt>get</tt> and <tt>put</tt>). The
|
||||
* expected number of entries in the map and its load factor should be taken
|
||||
* into account when setting its initial capacity, so as to minimize the
|
||||
* number of rehash operations. If the initial capacity is greater
|
||||
* than the maximum number of entries divided by the load factor, no
|
||||
* rehash operations will ever occur.
|
||||
*
|
||||
* <p>If many mappings are to be stored in a <tt>HashMap</tt> instance,
|
||||
* creating it with a sufficiently large capacity will allow the mappings to
|
||||
* be stored more efficiently than letting it perform automatic rehashing as
|
||||
* needed to grow the table.
|
||||
*
|
||||
* <p><strong>Note that this implementation is not synchronized.</strong>
|
||||
* If multiple threads access a hash map concurrently, and at least one of
|
||||
* the threads modifies the map structurally, it <i>must</i> be
|
||||
* synchronized externally. (A structural modification is any operation
|
||||
* that adds or deletes one or more mappings; merely changing the value
|
||||
* associated with a key that an instance already contains is not a
|
||||
* structural modification.) This is typically accomplished by
|
||||
* synchronizing on some object that naturally encapsulates the map.
|
||||
*
|
||||
* If no such object exists, the map should be "wrapped" using the
|
||||
* {@link Collections#synchronizedMap Collections.synchronizedMap}
|
||||
* method. This is best done at creation time, to prevent accidental
|
||||
* unsynchronized access to the map:<pre>
|
||||
* Map m = Collections.synchronizedMap(new HashMap(...));</pre>
|
||||
*
|
||||
* <p>The iterators returned by all of this class's "collection view methods"
|
||||
* are <i>fail-fast</i>: if the map is structurally modified at any time after
|
||||
* the iterator is created, in any way except through the iterator's own
|
||||
* <tt>remove</tt> method, the iterator will throw a
|
||||
* {@link ConcurrentModificationException}. Thus, in the face of concurrent
|
||||
* modification, the iterator fails quickly and cleanly, rather than risking
|
||||
* arbitrary, non-deterministic behavior at an undetermined time in the
|
||||
* future.
|
||||
*
|
||||
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
|
||||
* as it is, generally speaking, impossible to make any hard guarantees in the
|
||||
* presence of unsynchronized concurrent modification. Fail-fast iterators
|
||||
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
|
||||
* Therefore, it would be wrong to write a program that depended on this
|
||||
* exception for its correctness: <i>the fail-fast behavior of iterators
|
||||
* should be used only to detect bugs.</i>
|
||||
*
|
||||
* <p>This class is a member of the
|
||||
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
|
||||
* Java Collections Framework</a>.
|
||||
*
|
||||
* @param <K> the type of keys maintained by this map
|
||||
* @param <V> the type of mapped values
|
||||
*
|
||||
* @author Doug Lea
|
||||
* @author Josh Bloch
|
||||
* @author Arthur van Hoff
|
||||
* @author Neal Gafter
|
||||
* @see Object#hashCode()
|
||||
* @see Collection
|
||||
* @see Map
|
||||
* @see TreeMap
|
||||
* @see Hashtable
|
||||
* @since 1.2
|
||||
*/
|
||||
|
||||
public class HashMap<K,V>
|
||||
extends AbstractMap<K,V>
|
||||
implements Map<K,V>, Cloneable, Serializable
|
||||
|
@ -1,89 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package java.util;
|
||||
|
||||
/**
|
||||
* This class implements the <tt>Set</tt> interface, backed by a hash table
|
||||
* (actually a <tt>HashMap</tt> instance). It makes no guarantees as to the
|
||||
* iteration order of the set; in particular, it does not guarantee that the
|
||||
* order will remain constant over time. This class permits the <tt>null</tt>
|
||||
* element.
|
||||
*
|
||||
* <p>This class offers constant time performance for the basic operations
|
||||
* (<tt>add</tt>, <tt>remove</tt>, <tt>contains</tt> and <tt>size</tt>),
|
||||
* assuming the hash function disperses the elements properly among the
|
||||
* buckets. Iterating over this set requires time proportional to the sum of
|
||||
* the <tt>HashSet</tt> instance's size (the number of elements) plus the
|
||||
* "capacity" of the backing <tt>HashMap</tt> instance (the number of
|
||||
* buckets). Thus, it's very important not to set the initial capacity too
|
||||
* high (or the load factor too low) if iteration performance is important.
|
||||
*
|
||||
* <p><strong>Note that this implementation is not synchronized.</strong>
|
||||
* If multiple threads access a hash set concurrently, and at least one of
|
||||
* the threads modifies the set, it <i>must</i> be synchronized externally.
|
||||
* This is typically accomplished by synchronizing on some object that
|
||||
* naturally encapsulates the set.
|
||||
*
|
||||
* If no such object exists, the set should be "wrapped" using the
|
||||
* {@link Collections#synchronizedSet Collections.synchronizedSet}
|
||||
* method. This is best done at creation time, to prevent accidental
|
||||
* unsynchronized access to the set:<pre>
|
||||
* Set s = Collections.synchronizedSet(new HashSet(...));</pre>
|
||||
*
|
||||
* <p>The iterators returned by this class's <tt>iterator</tt> method are
|
||||
* <i>fail-fast</i>: if the set is modified at any time after the iterator is
|
||||
* created, in any way except through the iterator's own <tt>remove</tt>
|
||||
* method, the Iterator throws a {@link ConcurrentModificationException}.
|
||||
* Thus, in the face of concurrent modification, the iterator fails quickly
|
||||
* and cleanly, rather than risking arbitrary, non-deterministic behavior at
|
||||
* an undetermined time in the future.
|
||||
*
|
||||
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
|
||||
* as it is, generally speaking, impossible to make any hard guarantees in the
|
||||
* presence of unsynchronized concurrent modification. Fail-fast iterators
|
||||
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
|
||||
* Therefore, it would be wrong to write a program that depended on this
|
||||
* exception for its correctness: <i>the fail-fast behavior of iterators
|
||||
* should be used only to detect bugs.</i>
|
||||
*
|
||||
* <p>This class is a member of the
|
||||
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
|
||||
* Java Collections Framework</a>.
|
||||
*
|
||||
* @param <E> the type of elements maintained by this set
|
||||
*
|
||||
* @author Josh Bloch
|
||||
* @author Neal Gafter
|
||||
* @see Collection
|
||||
* @see Set
|
||||
* @see TreeSet
|
||||
* @see HashMap
|
||||
* @since 1.2
|
||||
*/
|
||||
|
||||
public class HashSet<E>
|
||||
extends AbstractSet<E>
|
||||
implements Set<E>, Cloneable, java.io.Serializable
|
||||
|
@ -1,149 +1,7 @@
|
||||
/*
|
||||
* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package java.util;
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* <p>Hash table and linked list implementation of the <tt>Map</tt> interface,
|
||||
* with predictable iteration order. This implementation differs from
|
||||
* <tt>HashMap</tt> in that it maintains a doubly-linked list running through
|
||||
* all of its entries. This linked list defines the iteration ordering,
|
||||
* which is normally the order in which keys were inserted into the map
|
||||
* (<i>insertion-order</i>). Note that insertion order is not affected
|
||||
* if a key is <i>re-inserted</i> into the map. (A key <tt>k</tt> is
|
||||
* reinserted into a map <tt>m</tt> if <tt>m.put(k, v)</tt> is invoked when
|
||||
* <tt>m.containsKey(k)</tt> would return <tt>true</tt> immediately prior to
|
||||
* the invocation.)
|
||||
*
|
||||
* <p>This implementation spares its clients from the unspecified, generally
|
||||
* chaotic ordering provided by {@link HashMap} (and {@link Hashtable}),
|
||||
* without incurring the increased cost associated with {@link TreeMap}. It
|
||||
* can be used to produce a copy of a map that has the same order as the
|
||||
* original, regardless of the original map's implementation:
|
||||
* <pre>
|
||||
* void foo(Map m) {
|
||||
* Map copy = new LinkedHashMap(m);
|
||||
* ...
|
||||
* }
|
||||
* </pre>
|
||||
* This technique is particularly useful if a module takes a map on input,
|
||||
* copies it, and later returns results whose order is determined by that of
|
||||
* the copy. (Clients generally appreciate having things returned in the same
|
||||
* order they were presented.)
|
||||
*
|
||||
* <p>A special {@link #LinkedHashMap(int,float,boolean) constructor} is
|
||||
* provided to create a linked hash map whose order of iteration is the order
|
||||
* in which its entries were last accessed, from least-recently accessed to
|
||||
* most-recently (<i>access-order</i>). This kind of map is well-suited to
|
||||
* building LRU caches. Invoking the <tt>put</tt> or <tt>get</tt> method
|
||||
* results in an access to the corresponding entry (assuming it exists after
|
||||
* the invocation completes). The <tt>putAll</tt> method generates one entry
|
||||
* access for each mapping in the specified map, in the order that key-value
|
||||
* mappings are provided by the specified map's entry set iterator. <i>No
|
||||
* other methods generate entry accesses.</i> In particular, operations on
|
||||
* collection-views do <i>not</i> affect the order of iteration of the backing
|
||||
* map.
|
||||
*
|
||||
* <p>The {@link #removeEldestEntry(Map.Entry)} method may be overridden to
|
||||
* impose a policy for removing stale mappings automatically when new mappings
|
||||
* are added to the map.
|
||||
*
|
||||
* <p>This class provides all of the optional <tt>Map</tt> operations, and
|
||||
* permits null elements. Like <tt>HashMap</tt>, it provides constant-time
|
||||
* performance for the basic operations (<tt>add</tt>, <tt>contains</tt> and
|
||||
* <tt>remove</tt>), assuming the hash function disperses elements
|
||||
* properly among the buckets. Performance is likely to be just slightly
|
||||
* below that of <tt>HashMap</tt>, due to the added expense of maintaining the
|
||||
* linked list, with one exception: Iteration over the collection-views
|
||||
* of a <tt>LinkedHashMap</tt> requires time proportional to the <i>size</i>
|
||||
* of the map, regardless of its capacity. Iteration over a <tt>HashMap</tt>
|
||||
* is likely to be more expensive, requiring time proportional to its
|
||||
* <i>capacity</i>.
|
||||
*
|
||||
* <p>A linked hash map has two parameters that affect its performance:
|
||||
* <i>initial capacity</i> and <i>load factor</i>. They are defined precisely
|
||||
* as for <tt>HashMap</tt>. Note, however, that the penalty for choosing an
|
||||
* excessively high value for initial capacity is less severe for this class
|
||||
* than for <tt>HashMap</tt>, as iteration times for this class are unaffected
|
||||
* by capacity.
|
||||
*
|
||||
* <p><strong>Note that this implementation is not synchronized.</strong>
|
||||
* If multiple threads access a linked hash map concurrently, and at least
|
||||
* one of the threads modifies the map structurally, it <em>must</em> be
|
||||
* synchronized externally. This is typically accomplished by
|
||||
* synchronizing on some object that naturally encapsulates the map.
|
||||
*
|
||||
* If no such object exists, the map should be "wrapped" using the
|
||||
* {@link Collections#synchronizedMap Collections.synchronizedMap}
|
||||
* method. This is best done at creation time, to prevent accidental
|
||||
* unsynchronized access to the map:<pre>
|
||||
* Map m = Collections.synchronizedMap(new LinkedHashMap(...));</pre>
|
||||
*
|
||||
* A structural modification is any operation that adds or deletes one or more
|
||||
* mappings or, in the case of access-ordered linked hash maps, affects
|
||||
* iteration order. In insertion-ordered linked hash maps, merely changing
|
||||
* the value associated with a key that is already contained in the map is not
|
||||
* a structural modification. <strong>In access-ordered linked hash maps,
|
||||
* merely querying the map with <tt>get</tt> is a structural
|
||||
* modification.</strong>)
|
||||
*
|
||||
* <p>The iterators returned by the <tt>iterator</tt> method of the collections
|
||||
* returned by all of this class's collection view methods are
|
||||
* <em>fail-fast</em>: if the map is structurally modified at any time after
|
||||
* the iterator is created, in any way except through the iterator's own
|
||||
* <tt>remove</tt> method, the iterator will throw a {@link
|
||||
* ConcurrentModificationException}. Thus, in the face of concurrent
|
||||
* modification, the iterator fails quickly and cleanly, rather than risking
|
||||
* arbitrary, non-deterministic behavior at an undetermined time in the future.
|
||||
*
|
||||
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
|
||||
* as it is, generally speaking, impossible to make any hard guarantees in the
|
||||
* presence of unsynchronized concurrent modification. Fail-fast iterators
|
||||
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
|
||||
* Therefore, it would be wrong to write a program that depended on this
|
||||
* exception for its correctness: <i>the fail-fast behavior of iterators
|
||||
* should be used only to detect bugs.</i>
|
||||
*
|
||||
* <p>This class is a member of the
|
||||
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
|
||||
* Java Collections Framework</a>.
|
||||
*
|
||||
* @param <K> the type of keys maintained by this map
|
||||
* @param <V> the type of mapped values
|
||||
*
|
||||
* @author Josh Bloch
|
||||
* @see Object#hashCode()
|
||||
* @see Collection
|
||||
* @see Map
|
||||
* @see HashMap
|
||||
* @see TreeMap
|
||||
* @see Hashtable
|
||||
* @since 1.4
|
||||
*/
|
||||
|
||||
public class LinkedHashMap<K,V>
|
||||
extends HashMap<K,V>
|
||||
implements Map<K,V>
|
||||
|
@ -1,120 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package java.util;
|
||||
|
||||
/**
|
||||
* <p>Hash table and linked list implementation of the <tt>Set</tt> interface,
|
||||
* with predictable iteration order. This implementation differs from
|
||||
* <tt>HashSet</tt> in that it maintains a doubly-linked list running through
|
||||
* all of its entries. This linked list defines the iteration ordering,
|
||||
* which is the order in which elements were inserted into the set
|
||||
* (<i>insertion-order</i>). Note that insertion order is <i>not</i> affected
|
||||
* if an element is <i>re-inserted</i> into the set. (An element <tt>e</tt>
|
||||
* is reinserted into a set <tt>s</tt> if <tt>s.add(e)</tt> is invoked when
|
||||
* <tt>s.contains(e)</tt> would return <tt>true</tt> immediately prior to
|
||||
* the invocation.)
|
||||
*
|
||||
* <p>This implementation spares its clients from the unspecified, generally
|
||||
* chaotic ordering provided by {@link HashSet}, without incurring the
|
||||
* increased cost associated with {@link TreeSet}. It can be used to
|
||||
* produce a copy of a set that has the same order as the original, regardless
|
||||
* of the original set's implementation:
|
||||
* <pre>
|
||||
* void foo(Set s) {
|
||||
* Set copy = new LinkedHashSet(s);
|
||||
* ...
|
||||
* }
|
||||
* </pre>
|
||||
* This technique is particularly useful if a module takes a set on input,
|
||||
* copies it, and later returns results whose order is determined by that of
|
||||
* the copy. (Clients generally appreciate having things returned in the same
|
||||
* order they were presented.)
|
||||
*
|
||||
* <p>This class provides all of the optional <tt>Set</tt> operations, and
|
||||
* permits null elements. Like <tt>HashSet</tt>, it provides constant-time
|
||||
* performance for the basic operations (<tt>add</tt>, <tt>contains</tt> and
|
||||
* <tt>remove</tt>), assuming the hash function disperses elements
|
||||
* properly among the buckets. Performance is likely to be just slightly
|
||||
* below that of <tt>HashSet</tt>, due to the added expense of maintaining the
|
||||
* linked list, with one exception: Iteration over a <tt>LinkedHashSet</tt>
|
||||
* requires time proportional to the <i>size</i> of the set, regardless of
|
||||
* its capacity. Iteration over a <tt>HashSet</tt> is likely to be more
|
||||
* expensive, requiring time proportional to its <i>capacity</i>.
|
||||
*
|
||||
* <p>A linked hash set has two parameters that affect its performance:
|
||||
* <i>initial capacity</i> and <i>load factor</i>. They are defined precisely
|
||||
* as for <tt>HashSet</tt>. Note, however, that the penalty for choosing an
|
||||
* excessively high value for initial capacity is less severe for this class
|
||||
* than for <tt>HashSet</tt>, as iteration times for this class are unaffected
|
||||
* by capacity.
|
||||
*
|
||||
* <p><strong>Note that this implementation is not synchronized.</strong>
|
||||
* If multiple threads access a linked hash set concurrently, and at least
|
||||
* one of the threads modifies the set, it <em>must</em> be synchronized
|
||||
* externally. This is typically accomplished by synchronizing on some
|
||||
* object that naturally encapsulates the set.
|
||||
*
|
||||
* If no such object exists, the set should be "wrapped" using the
|
||||
* {@link Collections#synchronizedSet Collections.synchronizedSet}
|
||||
* method. This is best done at creation time, to prevent accidental
|
||||
* unsynchronized access to the set: <pre>
|
||||
* Set s = Collections.synchronizedSet(new LinkedHashSet(...));</pre>
|
||||
*
|
||||
* <p>The iterators returned by this class's <tt>iterator</tt> method are
|
||||
* <em>fail-fast</em>: if the set is modified at any time after the iterator
|
||||
* is created, in any way except through the iterator's own <tt>remove</tt>
|
||||
* method, the iterator will throw a {@link ConcurrentModificationException}.
|
||||
* Thus, in the face of concurrent modification, the iterator fails quickly
|
||||
* and cleanly, rather than risking arbitrary, non-deterministic behavior at
|
||||
* an undetermined time in the future.
|
||||
*
|
||||
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
|
||||
* as it is, generally speaking, impossible to make any hard guarantees in the
|
||||
* presence of unsynchronized concurrent modification. Fail-fast iterators
|
||||
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
|
||||
* Therefore, it would be wrong to write a program that depended on this
|
||||
* exception for its correctness: <i>the fail-fast behavior of iterators
|
||||
* should be used only to detect bugs.</i>
|
||||
*
|
||||
* <p>This class is a member of the
|
||||
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
|
||||
* Java Collections Framework</a>.
|
||||
*
|
||||
* @param <E> the type of elements maintained by this set
|
||||
*
|
||||
* @author Josh Bloch
|
||||
* @see Object#hashCode()
|
||||
* @see Collection
|
||||
* @see Set
|
||||
* @see HashSet
|
||||
* @see TreeSet
|
||||
* @see Hashtable
|
||||
* @since 1.4
|
||||
*/
|
||||
|
||||
public class LinkedHashSet<E>
|
||||
extends HashSet<E>
|
||||
implements Set<E>, Cloneable, java.io.Serializable {
|
||||
|
@ -1,83 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package java.util;
|
||||
|
||||
/**
|
||||
* Doubly-linked list implementation of the {@code List} and {@code Deque}
|
||||
* interfaces. Implements all optional list operations, and permits all
|
||||
* elements (including {@code null}).
|
||||
*
|
||||
* <p>All of the operations perform as could be expected for a doubly-linked
|
||||
* list. Operations that index into the list will traverse the list from
|
||||
* the beginning or the end, whichever is closer to the specified index.
|
||||
*
|
||||
* <p><strong>Note that this implementation is not synchronized.</strong>
|
||||
* If multiple threads access a linked list concurrently, and at least
|
||||
* one of the threads modifies the list structurally, it <i>must</i> be
|
||||
* synchronized externally. (A structural modification is any operation
|
||||
* that adds or deletes one or more elements; merely setting the value of
|
||||
* an element is not a structural modification.) This is typically
|
||||
* accomplished by synchronizing on some object that naturally
|
||||
* encapsulates the list.
|
||||
*
|
||||
* If no such object exists, the list should be "wrapped" using the
|
||||
* {@link Collections#synchronizedList Collections.synchronizedList}
|
||||
* method. This is best done at creation time, to prevent accidental
|
||||
* unsynchronized access to the list:<pre>
|
||||
* List list = Collections.synchronizedList(new LinkedList(...));</pre>
|
||||
*
|
||||
* <p>The iterators returned by this class's {@code iterator} and
|
||||
* {@code listIterator} methods are <i>fail-fast</i>: if the list is
|
||||
* structurally modified at any time after the iterator is created, in
|
||||
* any way except through the Iterator's own {@code remove} or
|
||||
* {@code add} methods, the iterator will throw a {@link
|
||||
* ConcurrentModificationException}. Thus, in the face of concurrent
|
||||
* modification, the iterator fails quickly and cleanly, rather than
|
||||
* risking arbitrary, non-deterministic behavior at an undetermined
|
||||
* time in the future.
|
||||
*
|
||||
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
|
||||
* as it is, generally speaking, impossible to make any hard guarantees in the
|
||||
* presence of unsynchronized concurrent modification. Fail-fast iterators
|
||||
* throw {@code ConcurrentModificationException} on a best-effort basis.
|
||||
* Therefore, it would be wrong to write a program that depended on this
|
||||
* exception for its correctness: <i>the fail-fast behavior of iterators
|
||||
* should be used only to detect bugs.</i>
|
||||
*
|
||||
* <p>This class is a member of the
|
||||
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
|
||||
* Java Collections Framework</a>.
|
||||
*
|
||||
* @author Josh Bloch
|
||||
* @see List
|
||||
* @see ArrayList
|
||||
* @since 1.2
|
||||
* @param <E> the type of elements held in this collection
|
||||
*/
|
||||
|
||||
public class LinkedList<E>
|
||||
extends AbstractSequentialList<E>
|
||||
implements List<E>, Deque<E>, Cloneable, java.io.Serializable
|
||||
|
@ -1,82 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package java.util;
|
||||
|
||||
/**
|
||||
* An unbounded priority {@linkplain Queue queue} based on a priority heap.
|
||||
* The elements of the priority queue are ordered according to their
|
||||
* {@linkplain Comparable natural ordering}, or by a {@link Comparator}
|
||||
* provided at queue construction time, depending on which constructor is
|
||||
* used. A priority queue does not permit {@code null} elements.
|
||||
* A priority queue relying on natural ordering also does not permit
|
||||
* insertion of non-comparable objects (doing so may result in
|
||||
* {@code ClassCastException}).
|
||||
*
|
||||
* <p>The <em>head</em> of this queue is the <em>least</em> element
|
||||
* with respect to the specified ordering. If multiple elements are
|
||||
* tied for least value, the head is one of those elements -- ties are
|
||||
* broken arbitrarily. The queue retrieval operations {@code poll},
|
||||
* {@code remove}, {@code peek}, and {@code element} access the
|
||||
* element at the head of the queue.
|
||||
*
|
||||
* <p>A priority queue is unbounded, but has an internal
|
||||
* <i>capacity</i> governing the size of an array used to store the
|
||||
* elements on the queue. It is always at least as large as the queue
|
||||
* size. As elements are added to a priority queue, its capacity
|
||||
* grows automatically. The details of the growth policy are not
|
||||
* specified.
|
||||
*
|
||||
* <p>This class and its iterator implement all of the
|
||||
* <em>optional</em> methods of the {@link Collection} and {@link
|
||||
* Iterator} interfaces. The Iterator provided in method {@link
|
||||
* #iterator()} is <em>not</em> guaranteed to traverse the elements of
|
||||
* the priority queue in any particular order. If you need ordered
|
||||
* traversal, consider using {@code Arrays.sort(pq.toArray())}.
|
||||
*
|
||||
* <p> <strong>Note that this implementation is not synchronized.</strong>
|
||||
* Multiple threads should not access a {@code PriorityQueue}
|
||||
* instance concurrently if any of the threads modifies the queue.
|
||||
* Instead, use the thread-safe {@link
|
||||
* java.util.concurrent.PriorityBlockingQueue} class.
|
||||
*
|
||||
* <p>Implementation note: this implementation provides
|
||||
* O(log(n)) time for the enqueing and dequeing methods
|
||||
* ({@code offer}, {@code poll}, {@code remove()} and {@code add});
|
||||
* linear time for the {@code remove(Object)} and {@code contains(Object)}
|
||||
* methods; and constant time for the retrieval methods
|
||||
* ({@code peek}, {@code element}, and {@code size}).
|
||||
*
|
||||
* <p>This class is a member of the
|
||||
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
|
||||
* Java Collections Framework</a>.
|
||||
*
|
||||
* @since 1.5
|
||||
* @author Josh Bloch, Doug Lea
|
||||
* @param <E> the type of elements held in this collection
|
||||
*/
|
||||
public class PriorityQueue<E> extends AbstractQueue<E>
|
||||
implements java.io.Serializable {
|
||||
|
||||
|
@ -1,146 +1,6 @@
|
||||
/*
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is available under and governed by the GNU General Public
|
||||
* License version 2 only, as published by the Free Software Foundation.
|
||||
* However, the following notice accompanied the original version of this
|
||||
* file:
|
||||
*
|
||||
* Written by Doug Lea with assistance from members of JCP JSR-166
|
||||
* Expert Group and released to the public domain, as explained at
|
||||
* http://creativecommons.org/publicdomain/zero/1.0/
|
||||
*/
|
||||
|
||||
package java.util;
|
||||
|
||||
/**
|
||||
* A collection designed for holding elements prior to processing.
|
||||
* Besides basic {@link java.util.Collection Collection} operations,
|
||||
* queues provide additional insertion, extraction, and inspection
|
||||
* operations. Each of these methods exists in two forms: one throws
|
||||
* an exception if the operation fails, the other returns a special
|
||||
* value (either <tt>null</tt> or <tt>false</tt>, depending on the
|
||||
* operation). The latter form of the insert operation is designed
|
||||
* specifically for use with capacity-restricted <tt>Queue</tt>
|
||||
* implementations; in most implementations, insert operations cannot
|
||||
* fail.
|
||||
*
|
||||
* <p>
|
||||
* <table BORDER CELLPADDING=3 CELLSPACING=1>
|
||||
* <tr>
|
||||
* <td></td>
|
||||
* <td ALIGN=CENTER><em>Throws exception</em></td>
|
||||
* <td ALIGN=CENTER><em>Returns special value</em></td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td><b>Insert</b></td>
|
||||
* <td>{@link #add add(e)}</td>
|
||||
* <td>{@link #offer offer(e)}</td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td><b>Remove</b></td>
|
||||
* <td>{@link #remove remove()}</td>
|
||||
* <td>{@link #poll poll()}</td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td><b>Examine</b></td>
|
||||
* <td>{@link #element element()}</td>
|
||||
* <td>{@link #peek peek()}</td>
|
||||
* </tr>
|
||||
* </table>
|
||||
*
|
||||
* <p>Queues typically, but do not necessarily, order elements in a
|
||||
* FIFO (first-in-first-out) manner. Among the exceptions are
|
||||
* priority queues, which order elements according to a supplied
|
||||
* comparator, or the elements' natural ordering, and LIFO queues (or
|
||||
* stacks) which order the elements LIFO (last-in-first-out).
|
||||
* Whatever the ordering used, the <em>head</em> of the queue is that
|
||||
* element which would be removed by a call to {@link #remove() } or
|
||||
* {@link #poll()}. In a FIFO queue, all new elements are inserted at
|
||||
* the <em> tail</em> of the queue. Other kinds of queues may use
|
||||
* different placement rules. Every <tt>Queue</tt> implementation
|
||||
* must specify its ordering properties.
|
||||
*
|
||||
* <p>The {@link #offer offer} method inserts an element if possible,
|
||||
* otherwise returning <tt>false</tt>. This differs from the {@link
|
||||
* java.util.Collection#add Collection.add} method, which can fail to
|
||||
* add an element only by throwing an unchecked exception. The
|
||||
* <tt>offer</tt> method is designed for use when failure is a normal,
|
||||
* rather than exceptional occurrence, for example, in fixed-capacity
|
||||
* (or "bounded") queues.
|
||||
*
|
||||
* <p>The {@link #remove()} and {@link #poll()} methods remove and
|
||||
* return the head of the queue.
|
||||
* Exactly which element is removed from the queue is a
|
||||
* function of the queue's ordering policy, which differs from
|
||||
* implementation to implementation. The <tt>remove()</tt> and
|
||||
* <tt>poll()</tt> methods differ only in their behavior when the
|
||||
* queue is empty: the <tt>remove()</tt> method throws an exception,
|
||||
* while the <tt>poll()</tt> method returns <tt>null</tt>.
|
||||
*
|
||||
* <p>The {@link #element()} and {@link #peek()} methods return, but do
|
||||
* not remove, the head of the queue.
|
||||
*
|
||||
* <p>The <tt>Queue</tt> interface does not define the <i>blocking queue
|
||||
* methods</i>, which are common in concurrent programming. These methods,
|
||||
* which wait for elements to appear or for space to become available, are
|
||||
* defined in the {@link java.util.concurrent.BlockingQueue} interface, which
|
||||
* extends this interface.
|
||||
*
|
||||
* <p><tt>Queue</tt> implementations generally do not allow insertion
|
||||
* of <tt>null</tt> elements, although some implementations, such as
|
||||
* {@link LinkedList}, do not prohibit insertion of <tt>null</tt>.
|
||||
* Even in the implementations that permit it, <tt>null</tt> should
|
||||
* not be inserted into a <tt>Queue</tt>, as <tt>null</tt> is also
|
||||
* used as a special return value by the <tt>poll</tt> method to
|
||||
* indicate that the queue contains no elements.
|
||||
*
|
||||
* <p><tt>Queue</tt> implementations generally do not define
|
||||
* element-based versions of methods <tt>equals</tt> and
|
||||
* <tt>hashCode</tt> but instead inherit the identity based versions
|
||||
* from class <tt>Object</tt>, because element-based equality is not
|
||||
* always well-defined for queues with the same elements but different
|
||||
* ordering properties.
|
||||
*
|
||||
*
|
||||
* <p>This interface is a member of the
|
||||
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
|
||||
* Java Collections Framework</a>.
|
||||
*
|
||||
* @see java.util.Collection
|
||||
* @see LinkedList
|
||||
* @see PriorityQueue
|
||||
* @see java.util.concurrent.LinkedBlockingQueue
|
||||
* @see java.util.concurrent.BlockingQueue
|
||||
* @see java.util.concurrent.ArrayBlockingQueue
|
||||
* @see java.util.concurrent.LinkedBlockingQueue
|
||||
* @see java.util.concurrent.PriorityBlockingQueue
|
||||
* @since 1.5
|
||||
* @author Doug Lea
|
||||
* @param <E> the type of elements held in this collection
|
||||
*/
|
||||
public interface Queue<E> extends Collection<E> {
|
||||
/**
|
||||
* Inserts the specified element into this queue if it is possible to do so
|
||||
|
@ -1,50 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package java.util;
|
||||
|
||||
/**
|
||||
* The <code>Stack</code> class represents a last-in-first-out
|
||||
* (LIFO) stack of objects. It extends class <tt>Vector</tt> with five
|
||||
* operations that allow a vector to be treated as a stack. The usual
|
||||
* <tt>push</tt> and <tt>pop</tt> operations are provided, as well as a
|
||||
* method to <tt>peek</tt> at the top item on the stack, a method to test
|
||||
* for whether the stack is <tt>empty</tt>, and a method to <tt>search</tt>
|
||||
* the stack for an item and discover how far it is from the top.
|
||||
* <p>
|
||||
* When a stack is first created, it contains no items.
|
||||
*
|
||||
* <p>A more complete and consistent set of LIFO stack operations is
|
||||
* provided by the {@link Deque} interface and its implementations, which
|
||||
* should be used in preference to this class. For example:
|
||||
* <pre> {@code
|
||||
* Deque<Integer> stack = new ArrayDeque<Integer>();}</pre>
|
||||
*
|
||||
* @author Jonathan Payne
|
||||
* @since JDK1.0
|
||||
*/
|
||||
public
|
||||
class Stack<E> extends Vector<E> {
|
||||
/**
|
||||
|
@ -1,108 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package java.util;
|
||||
|
||||
/**
|
||||
* A Red-Black tree based {@link NavigableMap} implementation.
|
||||
* The map is sorted according to the {@linkplain Comparable natural
|
||||
* ordering} of its keys, or by a {@link Comparator} provided at map
|
||||
* creation time, depending on which constructor is used.
|
||||
*
|
||||
* <p>This implementation provides guaranteed log(n) time cost for the
|
||||
* {@code containsKey}, {@code get}, {@code put} and {@code remove}
|
||||
* operations. Algorithms are adaptations of those in Cormen, Leiserson, and
|
||||
* Rivest's <em>Introduction to Algorithms</em>.
|
||||
*
|
||||
* <p>Note that the ordering maintained by a tree map, like any sorted map, and
|
||||
* whether or not an explicit comparator is provided, must be <em>consistent
|
||||
* with {@code equals}</em> if this sorted map is to correctly implement the
|
||||
* {@code Map} interface. (See {@code Comparable} or {@code Comparator} for a
|
||||
* precise definition of <em>consistent with equals</em>.) This is so because
|
||||
* the {@code Map} interface is defined in terms of the {@code equals}
|
||||
* operation, but a sorted map performs all key comparisons using its {@code
|
||||
* compareTo} (or {@code compare}) method, so two keys that are deemed equal by
|
||||
* this method are, from the standpoint of the sorted map, equal. The behavior
|
||||
* of a sorted map <em>is</em> well-defined even if its ordering is
|
||||
* inconsistent with {@code equals}; it just fails to obey the general contract
|
||||
* of the {@code Map} interface.
|
||||
*
|
||||
* <p><strong>Note that this implementation is not synchronized.</strong>
|
||||
* If multiple threads access a map concurrently, and at least one of the
|
||||
* threads modifies the map structurally, it <em>must</em> be synchronized
|
||||
* externally. (A structural modification is any operation that adds or
|
||||
* deletes one or more mappings; merely changing the value associated
|
||||
* with an existing key is not a structural modification.) This is
|
||||
* typically accomplished by synchronizing on some object that naturally
|
||||
* encapsulates the map.
|
||||
* If no such object exists, the map should be "wrapped" using the
|
||||
* {@link Collections#synchronizedSortedMap Collections.synchronizedSortedMap}
|
||||
* method. This is best done at creation time, to prevent accidental
|
||||
* unsynchronized access to the map: <pre>
|
||||
* SortedMap m = Collections.synchronizedSortedMap(new TreeMap(...));</pre>
|
||||
*
|
||||
* <p>The iterators returned by the {@code iterator} method of the collections
|
||||
* returned by all of this class's "collection view methods" are
|
||||
* <em>fail-fast</em>: if the map is structurally modified at any time after
|
||||
* the iterator is created, in any way except through the iterator's own
|
||||
* {@code remove} method, the iterator will throw a {@link
|
||||
* ConcurrentModificationException}. Thus, in the face of concurrent
|
||||
* modification, the iterator fails quickly and cleanly, rather than risking
|
||||
* arbitrary, non-deterministic behavior at an undetermined time in the future.
|
||||
*
|
||||
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
|
||||
* as it is, generally speaking, impossible to make any hard guarantees in the
|
||||
* presence of unsynchronized concurrent modification. Fail-fast iterators
|
||||
* throw {@code ConcurrentModificationException} on a best-effort basis.
|
||||
* Therefore, it would be wrong to write a program that depended on this
|
||||
* exception for its correctness: <em>the fail-fast behavior of iterators
|
||||
* should be used only to detect bugs.</em>
|
||||
*
|
||||
* <p>All {@code Map.Entry} pairs returned by methods in this class
|
||||
* and its views represent snapshots of mappings at the time they were
|
||||
* produced. They do <strong>not</strong> support the {@code Entry.setValue}
|
||||
* method. (Note however that it is possible to change mappings in the
|
||||
* associated map using {@code put}.)
|
||||
*
|
||||
* <p>This class is a member of the
|
||||
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
|
||||
* Java Collections Framework</a>.
|
||||
*
|
||||
* @param <K> the type of keys maintained by this map
|
||||
* @param <V> the type of mapped values
|
||||
*
|
||||
* @author Josh Bloch and Doug Lea
|
||||
* @see Map
|
||||
* @see HashMap
|
||||
* @see Hashtable
|
||||
* @see Comparable
|
||||
* @see Comparator
|
||||
* @see Collection
|
||||
* @since 1.2
|
||||
*/
|
||||
|
||||
public class TreeMap<K,V>
|
||||
extends AbstractMap<K,V>
|
||||
implements NavigableMap<K,V>, Cloneable, java.io.Serializable
|
||||
|
@ -1,94 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package java.util;
|
||||
|
||||
/**
|
||||
* A {@link NavigableSet} implementation based on a {@link TreeMap}.
|
||||
* The elements are ordered using their {@linkplain Comparable natural
|
||||
* ordering}, or by a {@link Comparator} provided at set creation
|
||||
* time, depending on which constructor is used.
|
||||
*
|
||||
* <p>This implementation provides guaranteed log(n) time cost for the basic
|
||||
* operations ({@code add}, {@code remove} and {@code contains}).
|
||||
*
|
||||
* <p>Note that the ordering maintained by a set (whether or not an explicit
|
||||
* comparator is provided) must be <i>consistent with equals</i> if it is to
|
||||
* correctly implement the {@code Set} interface. (See {@code Comparable}
|
||||
* or {@code Comparator} for a precise definition of <i>consistent with
|
||||
* equals</i>.) This is so because the {@code Set} interface is defined in
|
||||
* terms of the {@code equals} operation, but a {@code TreeSet} instance
|
||||
* performs all element comparisons using its {@code compareTo} (or
|
||||
* {@code compare}) method, so two elements that are deemed equal by this method
|
||||
* are, from the standpoint of the set, equal. The behavior of a set
|
||||
* <i>is</i> well-defined even if its ordering is inconsistent with equals; it
|
||||
* just fails to obey the general contract of the {@code Set} interface.
|
||||
*
|
||||
* <p><strong>Note that this implementation is not synchronized.</strong>
|
||||
* If multiple threads access a tree set concurrently, and at least one
|
||||
* of the threads modifies the set, it <i>must</i> be synchronized
|
||||
* externally. This is typically accomplished by synchronizing on some
|
||||
* object that naturally encapsulates the set.
|
||||
* If no such object exists, the set should be "wrapped" using the
|
||||
* {@link Collections#synchronizedSortedSet Collections.synchronizedSortedSet}
|
||||
* method. This is best done at creation time, to prevent accidental
|
||||
* unsynchronized access to the set: <pre>
|
||||
* SortedSet s = Collections.synchronizedSortedSet(new TreeSet(...));</pre>
|
||||
*
|
||||
* <p>The iterators returned by this class's {@code iterator} method are
|
||||
* <i>fail-fast</i>: if the set is modified at any time after the iterator is
|
||||
* created, in any way except through the iterator's own {@code remove}
|
||||
* method, the iterator will throw a {@link ConcurrentModificationException}.
|
||||
* Thus, in the face of concurrent modification, the iterator fails quickly
|
||||
* and cleanly, rather than risking arbitrary, non-deterministic behavior at
|
||||
* an undetermined time in the future.
|
||||
*
|
||||
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
|
||||
* as it is, generally speaking, impossible to make any hard guarantees in the
|
||||
* presence of unsynchronized concurrent modification. Fail-fast iterators
|
||||
* throw {@code ConcurrentModificationException} on a best-effort basis.
|
||||
* Therefore, it would be wrong to write a program that depended on this
|
||||
* exception for its correctness: <i>the fail-fast behavior of iterators
|
||||
* should be used only to detect bugs.</i>
|
||||
*
|
||||
* <p>This class is a member of the
|
||||
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
|
||||
* Java Collections Framework</a>.
|
||||
*
|
||||
* @param <E> the type of elements maintained by this set
|
||||
*
|
||||
* @author Josh Bloch
|
||||
* @see Collection
|
||||
* @see Set
|
||||
* @see HashSet
|
||||
* @see Comparable
|
||||
* @see Comparator
|
||||
* @see TreeMap
|
||||
* @since 1.2
|
||||
*/
|
||||
|
||||
public class TreeSet<E> extends AbstractSet<E>
|
||||
implements NavigableSet<E>, Cloneable, java.io.Serializable
|
||||
{
|
||||
|
@ -1,81 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package java.util;
|
||||
|
||||
/**
|
||||
* The {@code Vector} class implements a growable array of
|
||||
* objects. Like an array, it contains components that can be
|
||||
* accessed using an integer index. However, the size of a
|
||||
* {@code Vector} can grow or shrink as needed to accommodate
|
||||
* adding and removing items after the {@code Vector} has been created.
|
||||
*
|
||||
* <p>Each vector tries to optimize storage management by maintaining a
|
||||
* {@code capacity} and a {@code capacityIncrement}. The
|
||||
* {@code capacity} is always at least as large as the vector
|
||||
* size; it is usually larger because as components are added to the
|
||||
* vector, the vector's storage increases in chunks the size of
|
||||
* {@code capacityIncrement}. An application can increase the
|
||||
* capacity of a vector before inserting a large number of
|
||||
* components; this reduces the amount of incremental reallocation.
|
||||
*
|
||||
* <p><a name="fail-fast"/>
|
||||
* The iterators returned by this class's {@link #iterator() iterator} and
|
||||
* {@link #listIterator(int) listIterator} methods are <em>fail-fast</em>:
|
||||
* if the vector is structurally modified at any time after the iterator is
|
||||
* created, in any way except through the iterator's own
|
||||
* {@link ListIterator#remove() remove} or
|
||||
* {@link ListIterator#add(Object) add} methods, the iterator will throw a
|
||||
* {@link ConcurrentModificationException}. Thus, in the face of
|
||||
* concurrent modification, the iterator fails quickly and cleanly, rather
|
||||
* than risking arbitrary, non-deterministic behavior at an undetermined
|
||||
* time in the future. The {@link Enumeration Enumerations} returned by
|
||||
* the {@link #elements() elements} method are <em>not</em> fail-fast.
|
||||
*
|
||||
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
|
||||
* as it is, generally speaking, impossible to make any hard guarantees in the
|
||||
* presence of unsynchronized concurrent modification. Fail-fast iterators
|
||||
* throw {@code ConcurrentModificationException} on a best-effort basis.
|
||||
* Therefore, it would be wrong to write a program that depended on this
|
||||
* exception for its correctness: <i>the fail-fast behavior of iterators
|
||||
* should be used only to detect bugs.</i>
|
||||
*
|
||||
* <p>As of the Java 2 platform v1.2, this class was retrofitted to
|
||||
* implement the {@link List} interface, making it a member of the
|
||||
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
|
||||
* Java Collections Framework</a>. Unlike the new collection
|
||||
* implementations, {@code Vector} is synchronized. If a thread-safe
|
||||
* implementation is not needed, it is recommended to use {@link
|
||||
* ArrayList} in place of {@code Vector}.
|
||||
*
|
||||
* @author Lee Boynton
|
||||
* @author Jonathan Payne
|
||||
* @see Collection
|
||||
* @see LinkedList
|
||||
* @since JDK1.0
|
||||
*/
|
||||
public class Vector<E>
|
||||
extends AbstractList<E>
|
||||
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
|
||||
|
Reference in New Issue
Block a user