Java Reference
In-Depth Information
Table 11.1
Useful Methods of the Collection Interface
Method
Description
add(element) Adds the specified element to this collection
addAll(collection) Adds all elements from the given collection to this collection
clear() Removes all elements from this collection
contains(element) Returns true if this collection contains the given element
containsAll(collection) Returns true if this collection contains all elements of the
given collection
Returns true if this collection contains no elements
isEmpty()
Returns an object that can be used to traverse the elements
of this collection
iterator()
Removes one occurrence of the specified element, if it is
contained in this collection
remove(element)
Removes all elements of the given collection from this
collection
removeAll(collection)
Removes all elements not found in the given collection from
this collection
retainAll(collection)
Returns the number of elements in this collection
size()
Returns an array containing the elements of this collection
toArray()
The java.util package contains an interface called Collection that is imple-
mented by all collections except Map . This interface specifies the operations that most
collections support. Table 11.1 lists those operations.
The Collection interface is extended and implemented by the other interfaces and
classes in the Java Collections Framework. Figure 11.1 summarizes the various inter-
faces and the classes that implement them, all of which will be discussed in this chapter.
LinkedList versus ArrayList
Now we'll look at a collection called LinkedList and compare and contrast it with
ArrayList .
<<interface>>
Collection
<<interface>>
Map
<<interface>>
List
<<interface>>
Set
ArrayList
LinkedList
HashSet
TreeSet
HashMap
TreeMap
Figure 11.1
An abridged view of the Java Collections Framework
 
Search WWH ::




Custom Search