×
☰ See All Chapters

Java Collection Framework

A collection sometimes called a container is simply an object that groups multiple elements /objects into a single unit. Collections allow us to add, retrieve update or delete data (CRUD operations) in primary memory using different data structures. The Collections Framework is a sophisticated hierarchy of interfaces and classes that provide state-of-the-art technology for managing groups of objects. Collection framework was added in java 1.2 version.

Key points to remember while working with collections

  1. Collections cannot store primitive data. If we store primitive data, Autoboxing will happen and will be converted to wrapper class object 

  2. All collection classes and interfaces are generic.  

  3. All collection classes (classes which are implementing Collection interface) can be converted to an array by calling toArray( ) method which is present in interface Collections. 

  4. All collections are eligible for cloning and serialization because all the collection classes are implementing two marker interfaces interface Cloneable & interface Serializable. 

History of Collection Framework

Java Version

Features Added

1.0

Vector, Stack, Dictionary, Hashtable, Properties, Enumerations

1.1

1.2

Collection Framework is introduced

1.3

 

1.4

 

1.5

Enhanced for loop, Autoboxing, Generics, PriorityQueue

1.6

NavigableSet, NavigableMap, Dque

1.7

Empty diamond syntax for generics

All collections frameworks contain the following

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

    • All the core collection interfaces are generic.  

Implementations

    • These are the concrete implementations of the collection interfaces. In essence, they are reusable data structures. 

    • All the classes in the collection framework implement the standard interfaces and hence provide a standard way to work with different data structures.  

    • All the classes in the collection framework are data engines encapsulating different data structures and have very  high efficiency. 

    • All the classes encapsulates their own data structures. For example class LinkedList encapsulates linked list data structure, class TreeSet encapsulates tree data structure, class ArrayList encapsulates array data structure, etc. 

    • Other classes and interfaces which are supporting collections in Java are: 

    1. Collections. (Class) 

    2. Enumeration.(Interface) 

    3. Iterator.(Interface) 

    4. ListIterator.(Interface) 

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. 

Benefits of Java Collections Framework

    • Reduced Development Effort – All collection framework classes and interfaces comes with almost all similar methods to iterate and manipulate the data. So we can concentrate more on business logic rather than designing our collection APIs. 

    • Increased Quality – All the data structures in collection framework are having strong efficiency, using collection framework increases our program quality rather than using any custom developed data structure. 

    • Increases performance - Collection framework has different flavor of data structures and all the data structures have the common parent. So parent collection class/interface can be used with various implementations. And we can change the implementation at later point just by changing in single line. We can use any implementation which well suits and tunes the application.  

    • Reduces the effort required to learn APIs by eliminating the need to learn multiple ad hoc collection APIs each one from scratch.  

    • Reduces the effort required to design and implement APIs by eliminating the need to produce ad hoc collections APIs. Designers and implementers don't have to reinvent the wheel each time they create an API that relies on collections; instead, they can use standard collection interfaces. 

There are really three overloaded uses of the word "collection"

    • collection (lowercase c), which represents any of the data structures in which objects are stored and iterated over. 

    • Collection (capital C), which is actually the java.util.Collection interface from which Set, List, and Queue extend. (That's right, extend, not implement. There are no direct implementations of Collection.)  

    • Collections (capital C and ends with s) is the java.util.Collections class that has static utility methods for use with collections. 

 

java-collection-framework-0
 
 java-collection-framework-1
 

Collections come in four basic flavors

  • Lists: Lists of things (classes that implement List). 

  • Sets: Set of unique things (classes that implement Set). 

  • Maps: Things with a unique ID (classes that implement Map). 

  • Queues: Things arranged by the order in which they are to be processed.                          

But there are sub-flavors within those four flavors of collections

java-collection-framework-2
 

The Methods Defined by Collection Interface

Modifier and Type

Description

boolean add(E obj)

Adds obj to the invoking collection. Returns true if obj was added to the collection. Returns false if obj is already a member of the collection and the collection does not allow duplicates.

boolean addAll(Collection<? extends E> c)

Adds all the elements of c to the invoking collection. Returns true if the operation succeeded (i.e., the elements were added). Otherwise, returns false.

void clear()

Removes all elements from the invoking collection.

boolean contains(Object obj)

Returns true if obj is an element of the invoking collection. Otherwise, returns false.

boolean containsAll(Collection<?> c)

Returns true if the invoking collection contains all elements of c. Otherwise, returns false.

boolean equals(Object obj)

Returns true if the invoking collection and obj are equal. Otherwise, returns false.

int hashCode()

Returns the hash code for the invoking collection.

boolean isEmpty()

Returns true if the invoking collection is empty. Otherwise, returns false.

Iterator<E> iterator()

Returns an iterator for the invoking collection

boolean remove(Object obj)

 

Removes one instance of obj from the invoking collection. Returns true if the element was removed. Otherwise, returns false.

boolean removeAll(Collection<?> c)

 

Removes all elements of c from the invoking collection. Returns true if the collection changed (i.e., elements were removed). Otherwise, returns false.

boolean retainAll(Collection<?> c)

 

Removes all elements from the invoking collection except those in c. Returns true if the collection changed (i.e., elements were removed). Otherwise, returns false.

int size()

Returns the number of elements held in the invoking collection.

Object[]toArray()

 

Returns an array that contains all the elements stored in the invoking collection. The array elements are copies of the collection elements.

<T> T[]toArray(T[] a)

 

Returns an array that contains the elements of the invoking collection. The array elements are copies of the collection elements. If the size of array equals the number of elements, these are returned in array. If the size of array is less than the number of elements, a new array of the necessary size is allocated and returned. If the size of array is greater than the number of elements, the array element following the last collection element is set to null. An ArrayStoreException is thrown if any collection element has a type that is not a subtype of array.

Since Collection interface is the super interface of all collection classes/interface methods listed above can be used in any collection classes.

 

 


All Chapters
Author