Collections are objects that act as containers of multiple variables of same type. Collections are used to dynamically store, retrieve and manipulate multiple values of the same type. We have already looked at one Collection like object – Arrays. But arrays fall short when the number of elements in the collection is not known in advance. In this and upcoming lessons we will be looking at the collections framework that was introduced in J2SE 1.2 (a.k.a Java 2)
Working with a group of similar variables or objects is one of the most common operations in programming. Therefore, Java provides you a set of collections and interfaces that makes manipulating a group of objects easier. These classes and interfaces are collectively referred to as Java Collections Framework.
Java Collections Framework
Java Collections Framework comprises of classes and interfaces that were introduced in Java 2 for purposes of handling multiple objects as a group. Prior to Java 2, Vector, Hashtable and Enumeration were the preferred way of dealing with collections. These classes were not optimized for performance as their individual methods required locking. Java Collections Framework was introduced to overcome these performance hurdles and provide a consistent way of using collections
Java Collections Framework classes and interface reside in the java.util package. The thread safe collections reside in java.util.concurrent package.
These classes and interfaces are so fundamental to Java programming that it is impossible to write any real world Java application without collections.
What Is a Collections Framework?
The Java Collections Framework comprises of the following three ingredients –
- Interfaces – interfaces define the abstract data type. They enable switching of implementations at runtime thus making applications flexible. True to Java’s paradigm of program to interfaces, the Java Collections Framework provides well defined interfaces and encourages programmers to use them wherever possible.
- Implementations in the form of classes – these are implementations of the interfaces.
- Algorithms – are methods or computations upon the data contained in collections. Iteration, sorting, searching, shuffling are some of the examples of algorithms/methods that can be applied on collections
Goals of Java Collections Framework
- Provide a way of dealing with group of objects
- Provide well designed, well implemented and well tested implementations of most common data structures and algorithms so that design, development and maintenance costs is reduced. Some of the data structures like lists and hashtables are so common that it makes more sense to ship implementations of these with the language
- Provide nearly uniform interface for using different collections. A uniform interface makes manipulation of various collections easy and at the same time makes the learning of the framework faster
Advantages of Java Collections Framework
- Reduction in development time – Java Collections Framework classes and interfaces provide implementation of the most common data structures and algorithms. This frees the developer to focus on the business logic
- Performance – Since Java Collections Framework implementation are optimized for performance, they will perform better than any other implementation
- Reduction in debugging times – Since Java Collections Framework classes and interfaces have been thoroughly tested, developers can focus on their business logic without having to worry about bugs in the collections’ implementation
- Reduction in maintenance costs – Since Java Collections Framework is ubiquitous any Java developer can gain mastery over an application that uses this framework