Java Reference
In-Depth Information
The Collections Framework provides implementations of collection interfaces, which are called implementation
classes. You need to create objects of these classes that will represent a collection. It is advised to write code using
interfaces, rather than using their implementation classes. The following snippet of code shows how to use the
implementation class ArrayList to create a list and store the reference in a variable of the type List that is the
interface representing a list:
// Create an instance of the ArrayList class storing the reference
// in a variable of the List interface
List<String> names = new ArrayList<>();
// Work with the names variable here onwards
Sometimes you need to perform different actions on a collection, such as searching through a collection,
converting a collection of one type to another type, copying elements from one collection to another, sorting elements
of a collection in a specific order, etc. The algorithm classes let you apply these kinds of algorithms to your collections.
Typically, you do not need to develop interfaces or classes in any of the three categories. The Collections
Framework provides you with all the interfaces and classes you need. You can choose from a variety of collection
interfaces and their implementations. Figure 12-2 shows the interfaces that define collections. I will discuss each type
of collection in the subsequent sections.
Figure 12-2. A class diagram including most interfaces in the Collections Framework
The Collection Interface
The Collection interface is the root of the collection interface hierarchy. It defines a generic collection. The
Collections Framework does not provide an implementation for the Collection interface. This is the most generic
type of collection. You can use it as an argument type in methods, where you do not care about the collection type of
the argument, provided it isn't a map. It declares methods that are inherited by other types of collection interfaces.
Non-map collection interfaces inherit from the Collection interface and add methods of their own to provide
functionalities that are specific to their types.
Methods of the Collection interface may be classified into the following categories:
Methods for basic operations
Methods for bulk (or group) operations
Methods for aggregate operations
 
Search WWH ::




Custom Search