What is Java Collections Framework?
In order to write efficient programs you need to know how to work with a data structure. A data structure is a entity which contains a group of data organized in some manner. Examples of popular data structures are arrays, stacks, queues, linked lists, trees, etc.
Java provides many data structures. The data structure framework or library provided by Java is known as the Java Collections Framework (JCF). Java defines a collection as an object that represents a group of objects.
The collections framework is a standard way for representing and manipulating collections. It contains the following components:
- Interfaces: These are abstract data types that represent collections. Interfaces allow collections to be manipulated independently of the details of their representation. In object-oriented languages, interfaces generally form a hierarchy.
- Classes: These are the concrete implementations of the collection interfaces. In essence, they are reusable data structures.
- Algorithms: These are the methods that perform useful computations, such as searching and sorting, on objects that implement collection interfaces. The algorithms are said to be polymorphic: that is, the same method can be used on many different implementations of the appropriate collection interface. In essence, algorithms are reusable functionality.
What Can I Do with a Collection?
- Add objects to the collection.
- Remove objects from the collection.
- Find out if an object (or group of objects) is in the collection.
- Retrieve an object from the collection (without removing it).
- Iterate through the collection, looking at each element (object) one after another.
Goals of Java Collections Framework
The collections framework was designed to meet the following goals:
- The framework had to be high-performance. The implementations for the fundamental collections (dynamic arrays, linked lists, trees, and hash tables) are highly efficient.
- The framework had to allow different types of collections to work in a similar manner and with a high degree of interoperability.
- Extending and/or adapting a collection had to be easy.
Interfaces in Java Collections Framework
Following is the Java Collections Framework hierarchy:
Following are various interfaces available in Java Collections Framework:
Classes in Java Collections Framework
Following are various classes available in Java Collections Framework: