Navratri Sale: Get Upto 30% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now

Collection Framework in Java

Level : Advanced
Mentor: Shailendra Chauhan
Duration : 00:09:00

What is a collection in Java?

A Java Collection of Objects refers to any set of independent objects represented as a single entity.

What is a Java framework?

A framework is a collection of classes and interfaces that form a ready-made architecture. A framework does not need to be defined before implementing a new feature or class.

What is a Collection Framework?

Collection Framework offers an architecture for storing and manipulating a set of objects. It makes it easier to develop code by offering data structures and algorithms. It delivers high-performance and high-quality data structures, enhancing speed. It promotes the reuse of standard data structures and methods.


Key Elements of Java Collection Framework

The Java Collection Framework consists of the following:

  • Interfaces
  • Classes
  • Algorithm

Advantages of a Collection Framework

  • Effort Reduction: Prebuilt data structures and algorithms reduce the need for manual coding.
  • Performance Boost: Provides high-performance implementations that improve program efficiency.
  • Interoperability: Enables easy data interchange between unrelated APIs.
  • Simplified Learning: Standardises collection handling to simplify the API learning process.
  • API Design Ease: Standardised collections make API development and execution easier.
  • Software Reusability: Promotes software reuse by standardizing collection interfaces and techniques.

Interfaces extending the Java Collections Interface

The collection framework includes several interfaces, each of which is used to store certain types of data. The following are the interfaces included in the framework:

  • Iterator Interface
  • Collection Interface
  • List Interface

Collection Interface

The Collection Interface is the core part of the Java Collections Framework, operating as the root interface. While no class directly implements it, it serves as the foundation for sub-interfaces such as List, Set, and Queue, which are then implemented by framework classes.

Iterator Interface

The Iterator interface allows for sequential traversal of a collection of elements. It is implemented by all collection classes, including ArrayList, LinkedList, HashSet, and TreeSet. 


Methods of Interator Interface

The Iterator interface includes 3 basic methods:

  1. hasNext()
  2. next()
  3. remove()


Need for Iterable

The Java Iterable interface offers uniform iteration over collection elements, which simplifies generic code. It facilitates traversal by allowing activities such as element reading, removal, and manipulation, while also serving as a universal cursor for the Collection API with user-friendly functions.

List Interface

The Java List interface provides ordered storage for elements, including duplicates, and is commonly utilized for data manipulation. It is found in the java.util package and inherits from the Collection interface, allowing for a standardized approach to organizing ordered collections.


Classes that implement the List interface

The classes implementing the List interface are as follows:

  • ArrayList
  • LinkedList
  • Vector
  • Stack

ArrayList

This class implements the List interface using a resizable array. It is identical to an array, except its size changes dynamically when elements are added or withdrawn.

ArrayList Methods


LinkedList

The LinkedList class implements the LinkedList data structure, which is a linear data structure in which items are not stored in contiguous locations and each element is a separate object having data and address parts. The elements are linked by pointers and addresses. Each element is called a node. 

Vector

In Java, we may create dynamic arrays using vectors. Though slower than standard arrays, it might be useful in programs that require a lot of array operations. This is implemented in the same way as ArrayList.

Stack

The Stack class defines and implements the Stack data structure. The class operates on the basic concept of last-in, first-out. In addition to the standard push and pop operations, the class includes three additional functions: empty, search, and peek. The class is also known as a subclass of Vector. 

Comparable Interface

The Comparable interface in Java, available in the java.lang package generates a natural ordering of objects through its single method, 'compareTo()'. This method compares items of the same type and returns a negative, zero, or positive integer based on their relative order.

Comparable vs. Comparator

Queue Interface

The Queue Interface implements the queue data structure's functionality. It is included in the java.util package and extends the Collection Interface. It is used to retain the elements that will be processed in the FIFO (First In, First Out) order.

Classes that implement the Queue interface

The classes implementing the Queue interface are as follows:

  • ArrayDeque
  •  LinkedList
  •  PriorityQueue

Set Interface

A set interface is an unordered collection of distinct items. The Java Collections Framework includes a built-in Set interface, which is implemented by several classes including HashSet, TreeSet, and LinkedHashSet.


HashSet

HashSet uses a process known as hashing to store its elements. HashSet is made up of unique components. The elements are sorted automatically.

LinkedHashSet

The LinkedHashSet class implements the set interface using a hash table and a linked list. It only contains unique elements, similar to HashSet. Linked HashSet additionally maintains insertion order.

HashSet and LinkedHashSet – Methods


TreeSet

The TreeSet class implements the Set interface and stores data in a tree structure. The objects of this class are stored in ascending order. The TreeSet class provides faster access and retrieval times.

TreeSet– Methods


Hash Tables

The Hashtable class in Java implements the Map interface to store key-value pairs using a hash table structure. It enforces non-null keys and values while ensuring thread safety through synchronization.

Map Interface

A Map is a collection of key-value pairs, with each key associated with a value. The Map interface maps unique keys to values.


Map - Methods


Map - Exceptions

Some exceptions are thrown when using the Maps interface:

  • NoSuchElementException
  • ClassCastException
  • NullPointerException
  • UnsupportedOperationException

HashMap, LinkedHashMap and TreeMap

HashMap is an implementation of the Map interface. In a HashMap, the keys and values can be any Object type, but the keys must be unique. The HashMap holds key-value pairs in a hash table, allowing for quick lookup and retrieval of items based on their keys.

Self-paced Membership
  • 24+ Video Courses
  • 825+ Hands-On Labs
  • 400+ Quick Notes
  • 125+ Skill Tests
  • 10+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this