javascript
Brief description  about Online courses   join in Online courses
OR

Collections Framework Tutorial

Mukul  Gosavi
Mukul Gosavi
Sr software Engineer

The collections framework was designed to meet several goals.

  1. The framework had to be high-performance. The implementations for the fundamental collections (dynamic arrays, linked lists, trees, and hash tables) are highly efficient.

  2. The framework had to allow different types of collections to work in a similar manner and with a high degree of interoperability.

  3. Extending and/or adapting a collection had to be easy.

Toward this end, the entire collections framework is designed around a set of standard interfaces. Several standard implementations such as LinkedList, HashSet, and TreeSet, of these interfaces are provided that you may use as-is and you may also implement your own collection, if you choose.

A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain the following:

  1. 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.

  2. Implementations i.e. Classes: These are the concrete implementations of the collection interfaces. In essence, they are reusable data structures.

  3. 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 addition to collections, the framework defines several map interfaces and classes. Maps store key/value pairs. Although maps are not collections in the proper use of the term, but they are fully integrated with collections.

This module takes you on an extended tour of the Collections Framework, first introduced with the Java 2 platform, Standard Edition, version 1.2. The Collections Framework provides a well-designed set of interfaces and classes for storing and manipulating groups of data as a single unit, a collection. The framework provides a convenient API to many of the abstract data types familiar from computer science data structure curriculum: maps, sets, lists, trees, arrays, hashtables and other collections. Because of their object-oriented design, the Java classes in the Collections Framework encapsulate both the data structures and the algorithms associated with these abstractions. The framework provides a standard programming interface to many of the most common abstractions, without burdening the programmer with too many procedures and interfaces. The operations supported by the collections framework nevertheless permit the programmer to easily define higher level data abstractions, such as stacks, queues, and thread-safe collections.

One thing worth noting early on is that while the framework is included with the Java 2 platform, a subset form is available for use with Java 1.1 runtime environments. 

Before diving into the Collections Framework, it helps to understand some of the terminology and set theory involved when working with the framework.

In the creation of the Collections Framework, the Sun development team needed to provide flexible interfaces that manipulated groups of elements. To keep the design simple, instead of providing separate interfaces for optional capabilities, the interfaces define all the methods an implementation class may provide. However, some of the interface methods are optional. Because an interface implementation must provide implementations for all the interface methods, there needed to be a way for a caller to know if an optional method is not supported. The manner the framework development team chose to signal callers when an optional method is called was to thrown an UnsupportedOperationException. If in the course of using a collection, an UnsupportedOperationException is thrown, like trying to add to a read-only collection, then the operation failed because it is not supported. To avoid having to place all collection operations within a try-catch block, the UnsupportedOperationException class is an extension of the RuntimeException class.

In addition to handling optional operations with a runtime exception, the iterators for the concrete collection implementations are fail-fast. That means that if you are using an Iterator to traverse a collection while underlying collection is being modified by another thread, then the Iterator fails immediately by throwing a ConcurrentModificationException (another RuntimeException). That means the next time an Iterator method is called, and the underlying collection has been modified, the ConcurrentModificationException exception gets thrown.

Write your comment now