What is Collection Framework? Explain the methods defined by the following interfaces

What is Collection Framework? Explain the methods defined by the following interfaces

  • Collection
  • List
  • Sorted Set
  • Queue

Answer:-

1. Collection Framework (Java)

The Java Collection Framework is a unified architecture for representing and manipulating collections of objects. It provides a set of interfaces and classes that help in storing, retrieving, and performing operations on data efficiently.
It is part of the java.util package.

Key Features:

  • Interfaces: Collection, List, Set, Queue, etc.
  • Classes: ArrayList, HashSet, LinkedList, PriorityQueue, etc.
  • Algorithms: Sorting, searching, shuffling, etc.
  • Benefits: Reusability, efficiency, and reduced programming effort.

2. Key Interfaces and Their Methods

a) Collection Interface

This is the root interface of the collection hierarchy (except for Map). It defines the basic operations applicable to all collections.

Common Methods:

  • boolean add(E e) – Adds an element.
  • boolean remove(Object o) – Removes a specific element.
  • boolean contains(Object o) – Checks if element exists.
  • int size() – Returns number of elements.
  • void clear() – Removes all elements.
  • boolean isEmpty() – Checks if collection is empty.
  • Iterator<E> iterator() – Returns an iterator to loop through elements.

All other interfaces like List, Set, and Queue extend this.


b) List Interface

The List interface extends Collection and represents an ordered collection (like an array). It allows duplicate elements and index-based access.

Important Methods:

  • void add(int index, E element) – Adds element at given index.
  • E get(int index) – Gets element at index.
  • E set(int index, E element) – Replaces element at index.
  • E remove(int index) – Removes element by index.
  • int indexOf(Object o) – First index of given element.
  • int lastIndexOf(Object o) – Last index of given element.

Implementations: ArrayList, LinkedList, Vector.


c) SortedSet Interface

The SortedSet interface extends Set and stores elements in sorted order (ascending by default). It does not allow duplicates.

Key Methods:

  • Comparator<? super E> comparator() – Returns custom comparator (if used).
  • E first() – Returns the first (lowest) element.
  • E last() – Returns the last (highest) element.
  • SortedSet<E> headSet(E toElement) – Elements less than toElement.
  • SortedSet<E> tailSet(E fromElement) – Elements greater than or equal to fromElement.
  • SortedSet<E> subSet(E from, E to) – Range view from from (inclusive) to to (exclusive).

Implementation: TreeSet.


d) Queue Interface

Queue extends Collection and represents a FIFO (First-In-First-Out) structure. It is mainly used for processing tasks in order.

Important Methods:

  • boolean offer(E e) – Adds an element.
  • E poll() – Removes and returns the head. Returns null if empty.
  • E peek() – Returns the head without removing. Returns null if empty.
  • E remove() – Removes and returns the head. Throws exception if empty.
  • E element() – Returns head without removing. Throws exception if empty.

Implementations: LinkedList, PriorityQueue.


3. Conclusion

The Java Collection Framework provides a powerful and flexible structure for handling groups of data. By understanding interfaces like Collection, List, SortedSet, and Queue, developers can choose the right data structure for a problem, ensuring cleaner, efficient, and maintainable code.

Leave a Reply

Your email address will not be published. Required fields are marked *