15.3. Comparing List
|
|
get |
add |
contains |
|
remove(0) |
iterator.remove |
|
|---|---|---|---|---|---|---|
|
ArrayList |
O (1) |
O (1) |
O ( n ) |
O (1) |
O ( n ) |
O ( n ) |
|
LinkedList |
O ( n ) |
O (1) |
O ( n ) |
O (1) |
O (1) |
O (1) |
|
CopyOnWrite-ArrayList |
O (1) |
O ( n ) |
O ( n ) |
O (1) |
O ( n ) |
O ( n ) |
For most list applications the choice is between
ArrayList
and
LinkedList
, synchronized or not. Once again, your decision will depend on how the list is used in practice. If
set
and
get
predominate, or element insertion and removal is
It is possible that, in a future release, ArrayDeque will be retrofitted to implement the List interface; if that happens, it will become the implementation of choice for both Queue and List in single-threaded environments.
Chapter 16. MapsThe Map interface is the last of the major Collections Framework interfaces, and the only one that does not inherit from Collection . It defines the operations that are supported by a set of key-to-value associations in which the keys are unique. These operations are shown in Figure 16.1 and fall into the following four groups, broadly parallel to the four operation groups of Collection adding elements, removing elements, querying collection contents, and providing different views of the contents of a collection. Figure 16-1. Map
Adding Associations
V put(K key, V value) // add or replace a key-value association
// return the old value (may be null) if the
// key was present; otherwise returns null
void putAll(Map<? extends K,? extends V> m)
// add each of the key-value associations in
// the supplied map into the receiver
The operations in this
Removing Associations
void clear() // remove all associations from this map
V remove(Object key) // remove the association, if any, with the
// given key; returns the value with which it
// was associated, or null
The signature of Map.remove is like that of the Collection.remove (see Section 12.1) in that it takes a parameter of type Object rather than the generic type. We discussed alternatives to this design in Section 2.6. Like the addition operations of the previous group, these removal operations are optional. Querying the Contents of a Map
V get(Object k) // return the value corresponding to k, or
// null if k is not present as a key
boolean containsKey(Object k) // return true if k is present as a key
boolean containsValue(Object v) // return true if v is present as a value
int size() // return the number of associations
boolean isEmpty() // return true if there are no associations
The arguments to
containsKey
and
containsValue
may be
null
for
Map
As with the
size
method of
Collection
, the largest element count that can be
Providing Collection Views of the Keys, Values, or Associations: Set<Map.Entry<K, V>> entrySet() // return a Set view of the associations Set<K> keySet() // return a Set view of the keys Collection<V> values() // return a Collection view of the values
The collections returned by these methods are
The
|