Java Collections Framework - Java

What is Java Collections Framework?

prior to Java 2, Java furnished ad hoc instructions such as Dictionary, Vector, Stack, and properties to store and manipulates groups of items. although these lessons had been pretty beneficial, they lacked a central, unifying topic. therefore, the way which you used Vector was exclusive from the manner which you used homes.

The collections framework become designed to meet several dreams, together with −

  • The framework had to be high-performance. The implementations for the fundamental collections (dynamic arrays, linked lists, bushes, and hashtables) were to be rather green.
  • The framework needed to permit exclusive types of collections to work in a comparable manner and with a high degree of interoperability.
  • The framework needed to enlarge and/or adapt a collection easily.

in the direction of this cease, the entire collections framework is designed round a hard and fast of wellknown interfaces. numerous popular implementations which includes LinkedList, HashSet, and TreeSet, of those interfaces are furnished that you can use as-is and you could also put in force your personal series, if you choose.

A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks include the subsequent −

  • Interfaces − these are abstract statistics kinds that constitute collections. Interfaces allow collections to be manipulated independently of the details in their illustration. In item-orientated languages, interfaces normally shape a hierarchy.
  • Implementations, i.e., training − those are the concrete implementations of the gathering interfaces. In essence, they're reusable statistics structures.
  • Algorithms − those are the methods that carry out useful computations, such as looking and sorting, on items that put into effect series interfaces. The algorithms are stated to be polymorphic: this is, the same technique can be used on many distinctive implementations of the best collection interface.

similarly to collections, the framework defines numerous map interfaces and training. Maps keep key/price pairs. although maps aren't collections inside the proper use of the term, however they're absolutely included with collections.

The Collection Interfaces

The collections framework defines several interfaces. This section provides an overview of each interface −


Interface & Description


The Collection Interface

This enables you to work with groups of objects; it is at the top of the collections hierarchy.


The List Interface

This extendsCollectionand an instance of List stores an ordered collection of elements.


The Set

This extends Collection to handle sets, which must contain unique elements.


The Sorted Set

This extends Set to handle sorted sets.


The Map

This maps unique keys to values.


The Map. Entry

This describes an element (a key/value pair) in a map. This is an inner class of Map.


The Sorted Map

This extends Map so that the keys are maintained in an ascending order.


The Enumeration

This is legacy interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects. This legacy interface has been superceded by Iterator.

The Collection Classes

Java provides a set of standard collection classes that implement Collection interfaces. Some of the classes provide full implementations that can be used as-is and others are abstract class, providing skeletal implementations that are used as starting points for creating concrete collections.

The standard collection classes are summarized in the following table −


Class & Description



Implements most of the Collection interface.



Extends AbstractCollection and implements most of the List interface.



Extends AbstractList for use by a collection that uses sequential rather than random access of its elements.


Linked List

Implements a linked list by extending AbstractSequentialList.


Array List

Implements a dynamic array by extending AbstractList.



Extends AbstractCollection and implements most of the Set interface.


Hash Set

Extends AbstractSet for use with a hash table.



Extends HashSet to allow insertion-order iterations.



Implements a set stored in a tree. Extends AbstractSet.



Implements most of the Map interface.



Extends AbstractMap to use a hash table.


Tree Map

Extends AbstractMap to use a tree.



Extends AbstractMap to use a hash table with weak keys.



Extends HashMap to allow insertion-order iterations.



Extends AbstractMap and uses reference equality when comparing documents.

The AbstractCollection, AbstractSet, AbstractList, AbstractSequentialList and AbstractMap classes provide skeletal implementations of the core collection interfaces, to minimize the effort required to implement them.

The following legacy classes defined by java.util have been discussed in the previous chapter −


Class & Description



This implements a dynamic array. It is similar to ArrayList, but with some differences.



Stack is a subclass of Vector that implements a standard last-in, first-out stack.



Dictionary is an abstract class that represents a key/value storage repository and operates much like Map.



Hashtable was part of the original java.util and is a concrete implementation of a Dictionary.



Properties is a subclass of Hashtable. It is used to maintain lists of values in which the key is a String and the value is also a String.



A BitSet class creates a special type of array that holds bit values. This array can increase in size as needed.

The Collection Algorithms

The collections framework defines several algorithms that can be applied to collections and maps. These algorithms are defined as static methods within the Collections class.

Several of the methods can throw a ClassCastException, which occurs when an attempt is made to compare incompatible types, or an UnsupportedOperationException, which occurs when an attempt is made to modify an unmodifiable collection.

Collections define three static variables: EMPTY_SET, EMPTY_LIST, and EMPTY_MAP. All are immutable.


Algorithm & Description


The Collection Algorithms

Here is a list of all the algorithm implementation.

How to Use an Iterator ?

Often, you will want to cycle through the elements in a collection. For example, you might want to display each element.
The easiest way to do this is to employ an iterator, which is an object that implements either the Iterator or the ListIterator interface.

Iterator enables you to cycle through a collection, obtaining or removing elements. ListIterator extends Iterator to allow bidirectional traversal of a list and the modification of elements.


Iterator Method & Description


Using Java Iterator

Here is a list of all the methods with examples provided by Iterator and ListIterator interfaces.

How to Use a Comparator ?

Both TreeSet and TreeMap store elements in a sorted order. However, it is the comparator that defines precisely what sorted order means.
This interface lets us sort a given collection any number of different ways. Also this interface can be used to sort any instances of any class (even classes we cannot modify).


Iterator Method & Description


Using Java Comparator

Here is a list of all the methods with examples provided by Comparator Interface.


The Java collections framework gives the programmer access to prepackaged data structures as well as to algorithms for manipulating them.
A collection is an object that can hold references to other objects. The collection interfaces declare the operations that can be performed on each type of collection.
The classes and interfaces of the collections framework are in package java.util.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

Java Topics