java.lang.Object
|
+--java.util.AbstractCollection
This class provides a skeletal implementation of the Collection interface, to minimize the effort required to implement this interface.
To implement an unmodifiable collection, the programmer needs only to extend this class and provide implementations for the iterator and size methods. (The iterator returned by the iterator method must implement hasNext and next.)
To implement a modifiable collection, the programmer must additionally override this class's add method (which otherwise throws an UnsupportedOperationException), and the iterator returned by the iterator method must additionally implement its remove method.
The programmer should generally provide a void (no argument) and Collection constructor, as per the recommendation in the Collection interface specification.
The documentation for each non-abstract methods in this class describes its implementation in detail. Each of these methods may be overridden if the collection being implemented admits a more efficient implementation.
java.util
Class AbstractList
java.lang.Object
|
+--java.util.AbstractCollection
|
+--java.util.AbstractList
This class provides a skeletal implementation of the List interface to minimize the effort required to implement this interface backed by a "random access" data store (such as an array). For sequential access data (such as a linked list), AbstractSequentialList should be used in preference to this class.
To implement an unmodifiable list, the programmer needs only to extend this class and provide implementations for the get(int index) and size() methods.
To implement a modifiable list, the programmer must additionally override the set(int index, Object element) method (which otherwise throws an UnsupportedOperationException. If the list is variable-size the programmer must additionally override the add(int index, Object element) and remove(int index) methods.
The programmer should generally provide a void (no argument) and collection constructor, as per the recommendation in the Collection interface specification.
Unlike the other abstract collection implementations, the programmer does not have to provide an iterator implementation; the iterator and list iterator are implemented by this class, on top the "random access" methods: get(int index), set(int index, Object element) , set(int index, Object element), add(int index, Object element) and remove(int index).
The documentation for each non-abstract methods in this class describes its implementation in detail. Each of these methods may be overridden if the collection being implemented admits a more efficient implementation.
java.lang.Object
|
+--java.util.AbstractCollection
|
+--java.util.AbstractList
|
+--java.util.ArrayList
Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list. (This class is roughly equivalent to Vector, except that it is unsynchronized.)
The size, isEmpty , get, set, iterator, and listIterator operations run in constant time. The add operation runs in amortized constant time, that is, adding n elements requires O(n) time. All of the other operations run in linear time (roughly speaking). The constant factor is low compared to that for the LinkedList implementation.
Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.
An application can increase the capacity of an ArrayList instance before adding a large number of elements using the ensureCapacity operation. This may reduce the amount of incremental reallocation.
Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; 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 Collections.synchronizedList method. This is best done at creation time, to prevent accidental unsynchronized access to the list:
List list = Collections.synchronizedList(new ArrayList(...));
The iterators returned by this class's iterator and listIterator methods are fail-fast : if list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a 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.
Collection
, List
, LinkedList
, Vector
,
Collections.synchronizedList(List)
,
Serialized FormFields inherited from class java.util.AbstractList |
modCount |
Constructor Summary | |
ArrayList() Constructs an empty list. |
|
ArrayList(Collection
c) Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator. |
|
ArrayList(int initialCapacity) Constructs an empty list with the specified initial capacity. |
Method Summary | |
void |
add(int index, Object
element) Inserts the specified element at the specified position in this list. |
boolean |
add(Object o) Appends the specified element to the end of this list. |
boolean |
addAll(Collection c) Appends all of the elements in the specified Collection to the end of this list, in the order that they are returned by the specified Collection's Iterator. |
boolean |
addAll(int index,
Collection c) Inserts all of the elements in the specified Collection into this list, starting at the specified position. |
void |
clear() Removes all of the elements from this list. |
Object |
clone() Returns a shallow copy of this ArrayList instance. |
boolean |
contains(Object elem) Returns true if this list contains the specified element. |
void |
ensureCapacity(int minCapacity) Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument. |
Object |
get(int index) Returns the element at the specified position in this list. |
int |
indexOf(Object elem) Searches for the first occurence of the given argument, testing for equality using the equals method. |
boolean |
isEmpty() Tests if this list has no elements. |
int |
lastIndexOf(Object elem) Returns the index of the last occurrence of the specified object in this list. |
Object |
remove(int index) Removes the element at the specified position in this list. |
protected void |
removeRange(int fromIndex, int toIndex) Removes from this List all of the elements whose index is between fromIndex, inclusive and toIndex, exclusive. |
Object |
set(int index, Object
element) Replaces the element at the specified position in this list with the specified element. |
int |
size() Returns the number of elements in this list. |
Object[] |
toArray() Returns an array containing all of the elements in this list in the correct order. |
Object[] |
toArray(Object[] a) Returns an array containing all of the elements in this list in the correct order. |
void |
trimToSize() Trims the capacity of this ArrayList instance to be the list's current size. |
Methods inherited from class java.util.AbstractList |
equals, hashCode
, iterator,
listIterator,
listIterator,
subList |
Methods inherited from class java.util.AbstractCollection |
containsAll,
remove,
removeAll,
retainAll,
toString |
Methods inherited from class java.lang.Object |
finalize, getClass
, notify,
notifyAll, wait,
wait, wait |
Methods inherited from interface java.util.List |
containsAll,
equals, hashCode,
iterator, listIterator
, listIterator
,
remove,
removeAll,
retainAll, subList |
The Stack
class represents a last-in-first-out (LIFO) stack of objects. It extends
class Vector with five operations that allow a vector to be treated
as a stack. The usual push and pop operations are provided,
as well as a method to peek at the top item on the stack, a method
to test for whether the stack is empty, and a method to search
the stack for an item and discover how far it is from the top.
When a stack is first created, it contains no items.
Fields inherited from class java.util.Vector |
capacityIncrement,
elementCount, elementData |
Fields inherited from class java.util. AbstractList |
modCount |
Constructor Summary | |
Stack() Creates an empty Stack. |
Method Summary | |
boolean |
empty() Tests if this stack is empty. |
Object |
peek() Looks at the object at the top of this stack without removing it from the stack. |
Object |
pop() Removes the object at the top of this stack and returns that object as the value of this function. |
Object |
push(Object item) Pushes an item onto the top of this stack. |
int |
search(Object o) Returns the 1-based position where an object is on this stack. |
Methods inherited from class java.util.Vector |
add, add
, addAll
,
addAll,
addElement, capacity
, clear,
clone,
contains,
containsAll,
copyInto, elementAt
, elements,
ensureCapacity,
equals, firstElement
, get,
hashCode,
indexOf,
indexOf,
insertElementAt, isEmpty
, lastElement,
lastIndexOf,
lastIndexOf, remove
, remove
, removeAll
, removeAllElements
, removeElement
, removeElementAt
, removeRange
, retainAll
, set
,
setElementAt, setSize
, size,
subList, toArray,
toArray, toString
, trimToSize |
Methods inherited from class java.util. AbstractList |
iterator,
listIterator,
listIterator |
Methods inherited from class java.lang.Object |
finalize, getClass
, notify,
notifyAll, wait,
wait, wait |
Methods inherited from interface java.util.List |
iterator, listIterator
, listIterator |
java.lang.Object
|
+--java.util.AbstractCollection
|
+--java.util.AbstractList
|
+--java.util.AbstractSequentialList
This class provides a skeletal implementation of the List interface to minimize the effort required to implement this interface backed by a "sequential access" data store (such as a linked list). For random access data (such as an array), AbstractList should be used in preference to this class.
This class is the opposite of the AbstractList class in the sense that it implements the "random access" methods (get(int index), set(int index, Object element), set(int index, Object element) , add(int index, Object element) and remove(int index)) on top of the list's list iterator, instead of the other way around.
To implement a list the programmer needs only to extend this class and provide implementations for the listIterator and size methods. For an unmodifiable list, the programmer need only implement the list iterator's hasNext , next, hasPrevious, previous and index methods.
For a modifiable list the programmer should additionally implement the list iterator's set method. For a variable-size list the programmer should additionally implement the list iterator's remove and add methods.
The programmer should generally provide a void (no argument) and collection constructor, as per the recommendation in the Collection interface specification.
java.lang.Object
|
+--java.util.AbstractCollection
|
+--java.util.AbstractList
|
+--java.util.AbstractSequentialList
|
+--java.util.LinkedList
Linked list implementation of the List interface. Implements all optional list operations, and permits all elements (including null ). In addition to implementing the List interface, the LinkedList class provides uniformly named methods to get, remove and insert an element at the beginning and end of the list. These operations allow linked lists to be used as a stack, queue, or double-ended queue (deque).
All of the stack/queue/deque operations could be easily recast in terms of the standard list operations. They're included here primarily for convenience, though they may run slightly faster than the equivalent List operations.
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 begining or the end, whichever is closer to the specified index.
Note that this implementation is not synchronized. If multiple threads access a list concurrently, and at least one of the threads modifies the list structurally, it must 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 Collections.synchronizedList method. This is best done at creation time, to prevent accidental unsynchronized access to the list:
List list = Collections.synchronizedList(new LinkedList(...));
The iterators returned by the this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the Iterator's own remove or add methods, the iterator will throw a 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.
List
, ArrayList
, Vector
,
Collections.synchronizedList(List)
,
Serialized FormFields inherited from class java.util. AbstractList |
modCount |
Constructor Summary | |
LinkedList() Constructs an empty list. |
|
LinkedList(Collection
c) Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator. |
Method Summary | |
void |
add(int index, Object
element) Inserts the specified element at the specified position in this list. |
boolean |
add(Object o) Appends the specified element to the end of this list. |
boolean |
addAll(Collection c) Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator. |
boolean |
addAll(int index,
Collection c) Inserts all of the elements in the specified collection into this list, starting at the specified position. |
void |
addFirst(Object o) Inserts the given element at the beginning of this list. |
void |
addLast(Object o) Appends the given element to the end of this list. |
void |
clear() Removes all of the elements from this list. |
Object |
clone() Returns a shallow copy of this LinkedList. |
boolean |
contains(Object o) Returns true if this list contains the specified element. |
Object |
get(int index) Returns the element at the specified position in this list. |
Object |
getFirst() Returns the first element in this list. |
Object |
getLast() Returns the last element in this list. |
int |
indexOf(Object o) Returns the index in this list of the first occurrence of the specified element, or -1 if the List does not contain this element. |
int |
lastIndexOf(Object o) Returns the index in this list of the last occurrence of the specified element, or -1 if the list does not contain this element. |
ListIterator |
listIterator(int index) Returns a list-iterator of the elements in this list (in proper sequence), starting at the specified position in the list. |
Object |
remove(int index) Removes the element at the specified position in this list. |
boolean |
remove(Object o) Removes the first occurrence of the specified element in this list. |
Object |
removeFirst() Removes and returns the first element from this list. |
Object |
removeLast() Removes and returns the last element from this list. |
Object |
set(int index, Object
element) Replaces the element at the specified position in this list with the specified element. |
int |
size
() Returns the number of elements in this list. |
Object[] |
toArray() Returns an array containing all of the elements in this list in the correct order. |
Object[] |
toArray(Object[] a) Returns an array containing all of the elements in this list in the correct order. |
Methods inherited from class java.util. AbstractSequentialList |
iterator |
Methods inherited from class java.util. AbstractList |
equals, hashCode
, listIterator
, removeRange
, subList |
Methods inherited from class java.util. AbstractCollection |
containsAll,
isEmpty,
removeAll,
retainAll,
toString |
Methods inherited from class java.lang. Object |
finalize
, getClass ,
notify, notifyAll
, wait,
wait, wait |
Methods inherited from interface java.util. List |
containsAll,
equals, hashCode,
isEmpty,
iterator,
listIterator,
removeAll,
retainAll, subList |
java.util
Class AbstractSet
java.lang.Object
|
+--java.util.AbstractCollection
|
+--java.util.AbstractSet
This class provides a skeletal implementation of the Set interface to minimize the effort required to implement this interface.
The process of implementing a set by extending this class is identical to that of implementing a Collection by extending AbstractCollection, except that all of the methods and constructors in subclasses of this class must obey the additional constraints imposed by the Set interface (for instance, the add method must not permit addition of multiple intances of an object to a set).
Note that this class does not override any of the implementations from the AbstractCollection class. It merely adds implementations for equals and hashCode.
java.lang.Object | +--java.util.AbstractCollection | +--java.util.AbstractSet | +--java.util.HashSet
This class implements the Set interface, backed by a hash table (actually a HashMap 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 null element.
This class offers constant time performance for the basic operations ( add, remove, contains and size), assuming the hash function disperses the elements properly among the buckets. Iterating over this set requires time proportional to the sum of the HashSet instance's size (the number of elements) plus the "capacity" of the backing HashMap instance (the number of buckets). Thus, it's very important not to set the intial capacity too high (or the load factor too low) if iteration performance is important.
Note that this implementation is not synchronized. If multiple threads
access a set concurrently, and at least one of the threads modifies the
set, it must 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 Collections.synchronizedSet
method. This is best done at creation time, to prevent accidental unsynchronized
access to the HashSet instance: Set s = Collections.synchronizedSet(new HashSet(...));
The iterators returned by this class's iterator method are fail-fast : if the set is modified at any time after the iterator is created, in any way except through the iterator's own remove method, the Iterator throws a 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.
Collection
, Set
, TreeSet
,
Collections.synchronizedSet(Set)
,
HashMap
,
Serialized FormConstructor Summary | |
HashSet() Constructs a new, empty set; the backing HashMap instance has default capacity and load factor, which is 0.75. |
|
HashSet(Collection c) Constructs a new set containing the elements in the specified collection. |
|
HashSet(int initialCapacity) Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and default load factor, which is 0.75. |
|
HashSet(int initialCapacity, float loadFactor) Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and the specified load factor. |
Method Summary | |
boolean |
add(Object o) Adds the specified element to this set if it is not already present. |
void |
clear() Removes all of the elements from this set. |
Object |
clone() Returns a shallow copy of this HashSet instance: the elements themselves are not cloned. |
boolean |
contains(Object o) Returns true if this set contains the specified element. |
boolean |
isEmpty() Returns true if this set contains no elements. |
Iterator |
iterator() Returns an iterator over the elements in this set. |
boolean |
remove(Object o) Removes the given element from this set if it is present. |
int |
size() Returns the number of elements in this set (its cardinality). |
Methods inherited from class java.util. AbstractSet |
equals, hashCode
,
removeAll |
Methods inherited from class java.util. AbstractCollection |
addAll,
containsAll,
retainAll,
toArray,
toArray,
toString |
Methods inherited from class java.lang. Object |
finalize, getClass
, notify,
notifyAll, wait,
wait, wait |
Methods inherited from interface java.util. Set |
addAll,
containsAll,
equals, hashCode,
removeAll,
retainAll, toArray,
toArray |
java.lang.Object | +--java.util.AbstractCollection | +--java.util.AbstractSet | +--java.util.TreeSet
This class implements the Set interface, backed by a TreeMap instance. This class guarantees that the sorted set will be in ascending element order, sorted according to the natural order of the elements (see Comparable), or by the comparator provided at set creation time, depending on which constructor is used.
This implementation provides guaranteed log(n) time cost for the basic operations (add, remove and contains).
Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equals if it is to correctly implement the Set interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Set interface is defined in terms of the equals operation, but a TreeSet instance performs all key comparisons using its compareTo (or compare) method, so two keys that are deemed equal by this method are, from the standpoint of the set, equal. The behavior of a set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Set interface.
Note that this implementation is not synchronized. If multiple threads
access a set concurrently, and at least one of the threads modifies the
set, it must 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 Collections.synchronizedSet
method. This is best done at creation time, to prevent accidental unsynchronized
access to the set: SortedSet s = Collections.synchronizedSortedSet(new TreeSet(...));
The Iterators returned by this class's iterator method are fail-fast : if the set is modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw a 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.
Collection
, Set
, HashSet
,
Comparable
, Comparator
,
Collections.synchronizedSortedSet(SortedSet)
,
TreeMap
,
Serialized FormConstructor Summary | |
TreeSet() Constructs a new, empty set, sorted according to the elements' natural order. |
|
TreeSet(Collection c) Constructs a new set containing the elements in the specified collection, sorted according to the elements' natural order. |
|
TreeSet(Comparator c) Constructs a new, empty set, sorted according to the given comparator. |
|
TreeSet(SortedSet s) Constructs a new set containing the same elements as the given sorted set, sorted according to the same ordering. |
Method Summary | |
boolean |
add(Object o) Adds the specified element to this set if it is not already present. |
boolean |
addAll(Collection c) Adds all of the elements in the specified collection to this set. |
void |
clear() Removes all of the elements from this set. |
Object |
clone() Returns a shallow copy of this TreeSet instance. |
Comparator |
comparator() Returns the comparator used to order this sorted set, or null if this tree set uses its elements natural ordering. |
boolean |
contains(Object o) Returns true if this set contains the specified element. |
Object |
first() Returns the first (lowest) element currently in this sorted set. |
SortedSet |
headSet(Object toElement) Returns a view of the portion of this set whose elements are strictly less than toElement . |
boolean |
isEmpty() Returns true if this set contains no elements. |
Iterator |
iterator() Returns an iterator over the elements in this set. |
Object |
last() Returns the last (highest) element currently in this sorted set. |
boolean |
remove(Object o) Removes the given element from this set if it is present. |
int |
size() Returns the number of elements in this set (its cardinality). |
SortedSet |
subSet(Object fromElement,
Object toElement) Returns a view of the portion of this set whose elements range from fromElement , inclusive, to toElement, exclusive. |
SortedSet |
tailSet(Object fromElement) Returns a view of the portion of this set whose elements are greater than or equal to fromElement. |
Methods inherited from class java.util. AbstractSet |
equals, hashCode
,
removeAll |
Methods inherited from class java.util. AbstractCollection |
containsAll,
retainAll,
toArray,
toArray,
toString |
Methods inherited from class java.lang. Object |
finalize, getClass
, notify,
notifyAll, wait,
wait, wait |
Methods inherited from interface java.util. Set |
containsAll,
equals, hashCode,
removeAll,
retainAll, toArray,
toArray |
java.util
Class AbstractMap
java.lang.Object
|
+--java.util.AbstractMap
This class provides a skeletal implementation of the Map interface, to minimize the effort required to implement this interface.
To implement an unmodifiable map, the programmer needs only to extend this class and provide an implementation for the entrySet method, which returns a set-view of the map's mappings. Typically, the returned set will, in turn, be implemented atop AbstractSet. This set should not support the add or remove methods, and its iterator should not support the remove method.
To implement a modifiable map, the programmer must additionally override this class's put method (which otherwise throws an UnsupportedOperationException ), and the iterator returned by entrySet().iterator() must additionally implement its remove method.
The programmer should generally provide a void (no argument) and map constructor, as per the recommendation in the Map interface specification.
The documentation for each non-abstract methods in this class describes its implementation in detail. Each of these methods may be overridden if the map being implemented admits a more efficient implementation.
java.lang.Object | +--java.util.AbstractMap | +--java.util.HashMap
Hash table based implementation of the Map interface. This implementation provides all of the optional map operations, and permits null values and the null key. (The HashMap class is roughly equivalent to Hashtable, 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.
This implementation provides constant-time performance for the basic operations (get and put), assuming the hash function disperses the elements properly among the buckets. Iteration over collection views requires time proportional to the "capacity" of the HashMap instance (the number of buckets) plus its size (the number of key-value mappings). Thus, it's very important not to set the intial capacity too high (or the load factor too low) if iteration performance is important.
An instance of HashMap has two parameters that affect its performance: initial capacity and load factor. The capacity 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 load factor 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 capacity is roughly doubled by calling the rehash method.
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 HashMap class, including get and put). 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.
If many mappings are to be stored in a HashMap 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.
Note that this implementation is not synchronized. If multiple threads
access this map concurrently, and at least one of the threads modifies the
map structurally, it must 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 Collections.synchronizedMap
method. This is best done at creation time, to prevent accidental unsynchronized
access to the map: Map m = Collections.synchronizedMap(new HashMap(...));
The iterators returned by all of this class's "collection view methods" are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a 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.
Object.hashCode()
,
Collection
, Map
, TreeMap
,
Hashtable
,
Serialized FormInner classes inherited from class java.util. Map |
Map.Entry |
Constructor Summary | |
HashMap() Constructs a new, empty map with a default capacity and load factor, which is 0.75 . |
|
HashMap(int initialCapacity) Constructs a new, empty map with the specified initial capacity and default load factor, which is 0.75. |
|
HashMap(int initialCapacity, float loadFactor) Constructs a new, empty map with the specified initial capacity and the specified load factor. |
|
HashMap(Map t) Constructs a new map with the same mappings as the given map. |
Method Summary | |
void |
clear() Removes all mappings from this map. |
Object |
clone() Returns a shallow copy of this HashMap instance: the keys and values themselves are not cloned. |
boolean |
containsKey(Object key) Returns true if this map contains a mapping for the specified key. |
boolean |
containsValue(Object value) Returns true if this map maps one or more keys to the specified value. |
Set |
entrySet() Returns a collection view of the mappings contained in this map. |
Object |
get(Object key) Returns the value to which this map maps the specified key. |
boolean |
isEmpty() Returns true if this map contains no key-value mappings. |
Set |
keySet() Returns a set view of the keys contained in this map. |
Object |
put(Object key,
Object value) Associates the specified value with the specified key in this map. |
void |
putAll(Map t) Copies all of the mappings from the specified map to this one. |
Object |
remove(Object key) Removes the mapping for this key from this map if present. |
int |
size() Returns the number of key-value mappings in this map. |
Collection |
values() Returns a collection view of the values contained in this map. |
Methods inherited from class java.util. AbstractMap |
equals, hashCode
, toString |
Methods inherited from class java.lang. Object |
finalize, getClass
, notify,
notifyAll, wait,
wait, wait |
Methods inherited from interface java.util. Map |
equals, hashCode |
java.lang.Object | +--java.util.AbstractMap | +--java.util.TreeMap
Red-Black tree based implementation of the SortedMap interface. This class guarantees that the map will be in ascending key order, sorted according to the natural order for the key's class (see Comparable ), or by the comparator provided at creation time, depending on which constructor is used.
This implementation provides guaranteed log(n) time cost for the containsKey , get, put and remove operations. Algorithms are adaptations of those in Cormen, Leiserson, and Rivest's Introduction to Algorithms.
Note that the ordering maintained by a sorted map (whether or not an explicit comparator is provided) must be consistent with equals if this sorted map is to correctly implement the Map interface. (See Comparable or Comparator for a precise definition of consistent with equals .) This is so because the Map interface is defined in terms of the equals operation, but a map performs all key comparisons using its compareTo (or 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 is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Map interface.
Note that this implementation is not synchronized. If multiple threads
access a map concurrently, and at least one of the threads modifies the
map structurally, it must 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 Collections.synchronizedMap method.
This is best done at creation time, to prevent accidental unsynchronized
access to the map: Map m = Collections.synchronizedMap(new TreeMap(...));
The iterators returned by all of this class's "collection view methods" are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator throws a 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.
Map
, HashMap
, Hashtable
,
Comparable
, Comparator
, Collection
,
Collections.synchronizedMap(Map)
,
Serialized FormInner classes inherited from class java.util. Map |
Map.Entry |
Constructor Summary | |
TreeMap() Constructs a new, empty map, sorted according to the keys' natural order. |
|
TreeMap(Comparator c) Constructs a new, empty map, sorted according to the given comparator. |
|
TreeMap(Map m) Constructs a new map containing the same mappings as the given map, sorted according to the keys' natural order. |
|
TreeMap(SortedMap m) Constructs a new map containing the same mappings as the given SortedMap, sorted according to the same ordering. |
Method Summary | |
void |
clear() Removes all mappings from this TreeMap. |
Object |
clone() Returns a shallow copy of this TreeMap instance. |
Comparator |
comparator() Returns the comparator used to order this map, or null if this map uses its keys' natural order. |
boolean |
containsKey(Object key) Returns true if this map contains a mapping for the specified key. |
boolean |
containsValue(Object value) Returns true if this map maps one or more keys to the specified value. |
Set |
entrySet() Returns a set view of the mappings contained in this map. |
Object |
firstKey() Returns the first (lowest) key currently in this sorted map. |
Object |
get(Object key) Returns the value to which this map maps the specified key. |
SortedMap |
headMap(Object toKey) Returns a view of the portion of this map whose keys are strictly less than toKey . |
Set |
keySet() Returns a Set view of the keys contained in this map. |
Object |
lastKey() Returns the last (highest) key currently in this sorted map. |
Object |
put(Object key,
Object value) Associates the specified value with the specified key in this map. |
void |
putAll(Map map) Copies all of the mappings from the specified map to this map. |
Object |
remove(Object key) Removes the mapping for this key from this TreeMap if present. |
int |
size() Returns the number of key-value mappings in this map. |
SortedMap |
subMap(Object fromKey,
Object toKey) Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive. |
SortedMap |
tailMap(Object fromKey) Returns a view of the portion of this map whose keys are greater than or equal to fromKey . |
Collection |
values() Returns a collection view of the values contained in this map. |
Methods inherited from class java.util. AbstractMap |
equals, hashCode
, isEmpty,
toString |
Methods inherited from class java.lang. Object |
finalize, getClass
, notify,
notifyAll, wait,
wait, wait |
Methods inherited from interface java.util. Map |
equals, hashCode,
isEmpty |