Curriculum
Course: Learn Java Programming
Login

Curriculum

Learn Java Programming

Text lesson

Overview of Collection Framework in Java

[post-views]

 

In this lesson, you will learn.

  • Java Collection Framework and its Components
  • Hierarchy of Collection Framework

 

Introduction to Java Collection Framework

  1. The Java Collection Framework provides a set of interfaces and classes to handle groups of objects as a single unit, referred to as collections.
  2. It is part of the java.util package.

 

Components of Collection Framework

The Java Collection Framework consists of three major components:

 

 

  1. Interfaces: Define the abstract data types for collections.
  2. Classes (Implementations): Provide concrete implementations of the collection interfaces.
  3. Algorithms: Provide static methods for performing common tasks such as sorting, searching, and modifying collections.

 

Hierarchy of Java Collection Framework

Let us see the Java collections framework hierarchy.

 

 

Interfaces: Collection Framework

 

Iterator Interface

  1. Iterator is used to traverse elements of a collection, such as lists, sets, and maps.
  2. The Iterator provides a way to access elements of a collection sequentially without exposing the underlying structure.

 

Iterator has the following three methods

  1. boolean hasNext(): Returns true if the iteration has more elements.
  2. E next(): Returns the next element in the iteration
  3. void remove(): Removes the last element returned by the iterator (optional operation).

 

Collection Interface

  1. It is the root interface of the Java Collection Framework.
  2. It defines a group of objects known as elements.
  3. The Collection interface is implemented by various sub-interfaces like List, Set, and Queue

 

Below is a summary table listing the common methods provided by the Collection interface in Java:

Method Signature Description
boolean add(E e) Adds the specified element to the collection (optional operation).
void clear() Removes all of the elements from this collection (optional operation).
boolean contains(Object o) Returns true if this collection contains the specified element.
boolean equals(Object o) Compares the specified object with this collection for equality.
boolean isEmpty() Returns true if this collection contains no elements.
Iterator<E> iterator() Returns an iterator over the elements in this collection.
boolean remove(Object o) Removes a single instance of the specified element from this collection, if it is present (optional operation).
int size() Returns the number of elements in this collection.
Object[] toArray() Returns an array containing all of the elements in this collection.

 

The List Interface

  1. Ordered Collection: Elements are stored in a specific order.
  2. Allows Duplicates: A List can contain duplicate elements.
  3. Index-Based Access: Elements can be accessed by their index positions.

 

List interface includes the following classes

  1. Array List
  2. LinkedList
  3. Vector
  4. Stack

 

ArrayList and LinkedList are widely used in Java programming. The Vector class has been deprecated since Java 5.

 

The Set Interface

It extends the Collection interface and represents a collection that cannot contain duplicate elements.

 

Key Characteristics of Set Interface:

  1. No Duplicates: A Set does not allow duplicate elements. Each element in a Set must be unique.
  2. Unordered Collection: The elements in a Set are not stored in any particular order. However, some implementations, like LinkedHashSet, maintain insertion order.
  3. Allows Null Elements: Some implementations of Set allow one null element (e.g., HashSet), but some do not (e.g., TreeSet).

 

There are three main implementations of the Set interface:

  1. HashSet,
  2. TreeSet, and
  3. LinkedHashSet.

 

The Queue Interface

  1. It represents a collection designed for holding elements before processing.
  2. It follows the First-In-First-Out (FIFO) principle, although variations like priority queues may not strictly follow FIFO.

 

The Queue interface has the following classes

  1. LinkedList
  2. PriorityQueue
  3. ArrayDequeue

 

The Map Interface

  1. It represents a collection of key-value pairs. It maps keys to values, with no duplicate keys allowed, and each key can map to at most one value.

 

Classes Implementing Map Interface

  1. HashMap
  2. LinkedHashMap
  3. TreeMap

 

Summary Table of Interfaces

Interface Description Key Methods
Iterator An object for iterating over a collection. hasNext(), next(), remove()
Collection The root interface in the collection hierarchy. add(E e), remove(Object o), size(), clear(), contains(Object o), isEmpty(), iterator()
List An ordered collection (sequence) that allows duplicate elements. get(int index), set(int index, E element), add(int index, E element), remove(int index), indexOf(Object o), lastIndexOf(Object o), listIterator()
Set A collection that does not allow duplicate elements. Inherited methods from Collection: add(E e), remove(Object o), size(), clear(), contains(Object o), isEmpty(), iterator()
SortedSet A Set that maintains its elements in ascending order. first(), last(), comparator(), subSet(E fromElement, E toElement), headSet(E toElement), tailSet(E fromElement)
NavigableSet A SortedSet with navigation methods reporting closest matches for given search targets. lower(E e), floor(E e), ceiling(E e), higher(E e), pollFirst(), pollLast(), descendingSet()
Queue A collection is used to hold multiple elements before processing, typically in a FIFO (First-In-First-Out) manner. offer(E e), poll(), peek(), remove(), element()
Deque A double-ended queue that allows element insertion and removal at both ends. addFirst(E e), addLast(E e), removeFirst(), removeLast(), getFirst(), getLast(), offerFirst(E e), offerLast(E e), pollFirst(), pollLast(), peekFirst(), peekLast()
Map An object that maps keys to values, with no duplicate keys allowed. put(K key, V value), get(Object key), remove(Object key), containsKey(Object key), containsValue(Object value), keySet(), values(), entrySet()
SortedMap A Map that maintains its mappings in ascending key order. comparator(), subMap(K fromKey, K toKey), headMap(K toKey), tailMap(K fromKey), firstKey(), lastKey()
NavigableMap A SortedMap with navigation methods returning the closest matches for given search targets. lowerEntry(K key), floorEntry(K key), ceilingEntry(K key), higherEntry(K key), firstEntry(), lastEntry(), pollFirstEntry(), pollLastEntry(), descendingMap()
ListIterator An Iterator for lists that allows bidirectional traversal and modification of elements. hasPrevious(), previous(), add(E e), set(E e), previousIndex(), nextIndex()
Comparable Provides a natural ordering for objects of a class. compareTo(T o)
Comparator Defines a custom ordering for objects. compare(T o1, T o2), reversed()

 

 

 


 

End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

 

 

 

There are no reviews yet. Be the first one to write one.

 

 

Submit a Review

 

 

Layer 1
Login Categories