Java Reference
In-Depth Information
seen interfaces yet, but what this means is that all of the following methods are available for each of
these classes (as illustrated in the last Try It Out):
➤
boolean add(E e)
: Adds an element; returns
true
if the collection changed.
➤
boolean addAll(Collection<? extends E> c)
: Adds all elements from the given collec-
tion to this collection; returns
true
if the collection changed.
➤
void clear()
: Removes all elements from the collection.
➤
boolean contains(Object o)
: Returns
true
if the collection contains the given object,
false
otherwise.
➤
boolean containsAll(Collection<?> c)
: Returns
true
if this collection contains all of
the elements in the given collection,
false
otherwise.
➤
boolean equals(Object o)
: Performs an equality comparison.
➤
boolean isEmpty()
: Returns
true
if the collection is empty (contains no elements).
➤
boolean remove(Object o)
: Removes the given element from the collection; returns
true
if the collection changed.
➤
boolean removeAll(Collection<?> c)
: Removes all elements in the given collection from
this collection; returns
true
if the collection changed.
➤
boolean retainAll(Collection<?> c)
: Keeps only the elements in this collection that are
contained in the given collection; returns
true
if the collection changed.
➤
int size()
: Returns the number of elements in the collection.
➤
Object[] toArray()
: Returns an array containing all the elements in the collection.
➤
<T> T[] toArray(T[] a)
: Returns an array containing all the elements in the collection;
the type of the array will be that of the specified array.
Note that maps use a different set of methods:
➤
void clear()
: Removes all mappings from the map.
➤
boolean containsKey(Object key)
: Returns
true
if the map contains the specified key,
false
otherwise.
➤
boolean containsValue(Object value)
: Returns
true
if the map contains the specified
value,
false
otherwise.
➤
Set<Map.Entry<K,V>> entrySet()
: Returns a set containing
Entry
objects. Each
Entry
object models a key and value pair.
➤
boolean equals(Object o)
: Performs an equality comparison.
➤
V get(Object key)
: Returns the value mapped to the key, or
null
if the key is not present.
➤
boolean isEmpty()
: Returns
true
if the map contains no mappings,
false
otherwise.
➤
Set<K> keySet()
: Returns a set containing all the keys contained in this map.
➤
V put(K key, V value)
: Inserts a key/value pair; if the key exists, the value will be
overridden.
Search WWH ::
Custom Search