DEV Community

Abhishek Kumar
Abhishek Kumar

Posted on

Overview of Java Collection Interfaces

In Java, the Collection Framework provides a set of classes and interfaces to manage and manipulate groups of objects in a systematic and efficient way. The framework is primarily made up of multiple interfaces, each serving a different purpose for handling collections of data.

Here’s a quick overview of the core Collection Interfaces and their functionality:


1. Collection Interface (java.util.Collection)

  • Super Interface: This is the root interface of the Java Collection Framework. It represents a group of objects, known as elements.
  • Key Methods:
    • add(E e): Adds an element to the collection.
    • remove(Object o): Removes an element from the collection.
    • size(): Returns the number of elements in the collection.
    • clear(): Removes all elements from the collection.
    • iterator(): Returns an iterator for the collection.

Subinterfaces:

  • Set
  • List
  • Queue

2. List Interface (java.util.List)

  • Ordered Collection: It is an ordered collection (sequence) of elements where duplicates are allowed. Elements in a list are indexed and can be accessed by their position.
  • Common Implementations: ArrayList, LinkedList, Vector
  • Key Methods:
    • get(int index): Returns the element at the specified position.
    • set(int index, E element): Replaces the element at the specified position.
    • add(int index, E element): Inserts an element at the specified position.
    • remove(int index): Removes the element at the specified position.

Example:

List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
System.out.println(list.get(0));  // Output: Apple
Enter fullscreen mode Exit fullscreen mode

3. Set Interface (java.util.Set)

  • Unordered Collection: Represents a collection that does not allow duplicates. It models the mathematical set abstraction.
  • Common Implementations: HashSet, LinkedHashSet, TreeSet
  • Key Methods:
    • add(E e): Adds an element to the set (if it is not already present).
    • remove(Object o): Removes the specified element if present.
    • No duplicate elements allowed.

Example:

Set<String> set = new HashSet<>();
set.add("Apple");
set.add("Apple");  // Duplicate will not be added
System.out.println(set.size());  // Output: 1
Enter fullscreen mode Exit fullscreen mode

4. Queue Interface (java.util.Queue)

  • FIFO (First In, First Out): It represents a collection designed for holding elements prior to processing. Typically, queues follow FIFO order.
  • Common Implementations: LinkedList, PriorityQueue, ArrayDeque
  • Key Methods:
    • offer(E e): Inserts the element into the queue.
    • poll(): Retrieves and removes the head of the queue.
    • peek(): Retrieves, but does not remove, the head of the queue.

Example:

Queue<String> queue = new LinkedList<>();
queue.offer("Apple");
queue.offer("Banana");
System.out.println(queue.poll());  // Output: Apple
Enter fullscreen mode Exit fullscreen mode

5. Map Interface (java.util.Map)

  • Key-Value Pairs: A map is a collection that maps keys to values. It cannot contain duplicate keys, and each key maps to at most one value.
  • Common Implementations: HashMap, LinkedHashMap, TreeMap
  • Key Methods:
    • put(K key, V value): Associates the specified value with the specified key.
    • get(Object key): Returns the value mapped to the key.
    • remove(Object key): Removes the mapping for the key.
    • keySet(): Returns a set view of the keys contained in the map.

Example:

Map<String, Integer> map = new HashMap<>();
map.put("Apple", 1);
map.put("Banana", 2);
System.out.println(map.get("Apple"));  // Output: 1
Enter fullscreen mode Exit fullscreen mode

6. Deque Interface (java.util.Deque)

  • Double-Ended Queue: Deque stands for "double-ended queue" and supports element insertion and removal at both ends (head and tail).
  • Common Implementations: ArrayDeque, LinkedList
  • Key Methods:
    • addFirst(E e): Inserts the element at the front of the deque.
    • addLast(E e): Inserts the element at the end of the deque.
    • removeFirst(): Removes the first element.
    • removeLast(): Removes the last element.

Example:

Deque<String> deque = new ArrayDeque<>();
deque.addFirst("Apple");
deque.addLast("Banana");
System.out.println(deque.pollFirst());  // Output: Apple
Enter fullscreen mode Exit fullscreen mode

7. SortedSet Interface (java.util.SortedSet)

  • Ordered Set: A specialized set that maintains elements in a sorted order (by natural ordering or a custom comparator).
  • Common Implementation: TreeSet
  • Key Methods:
    • first(): Returns the first (lowest) element.
    • last(): Returns the last (highest) element.
    • subSet(E fromElement, E toElement): Returns a view of the portion of this set whose elements range between the two specified elements.

Example:

SortedSet<Integer> sortedSet = new TreeSet<>();
sortedSet.add(5);
sortedSet.add(1);
sortedSet.add(3);
System.out.println(sortedSet.first());  // Output: 1
Enter fullscreen mode Exit fullscreen mode

8. SortedMap Interface (java.util.SortedMap)

  • Ordered Map: A specialized map that maintains keys in a sorted order (by natural ordering or a comparator).
  • Common Implementation: TreeMap
  • Key Methods:
    • firstKey(): Returns the first (lowest) key.
    • lastKey(): Returns the last (highest) key.
    • subMap(K fromKey, K toKey): Returns a view of the portion of this map whose keys range between fromKey and toKey.

Example:

SortedMap<Integer, String> sortedMap = new TreeMap<>();
sortedMap.put(3, "Apple");
sortedMap.put(1, "Banana");
sortedMap.put(2, "Cherry");
System.out.println(sortedMap.firstKey());  // Output: 1
Enter fullscreen mode Exit fullscreen mode

Summary of Key Interfaces:

Interface Description Common Implementations
Collection Root interface for all collections -
List Ordered collection with duplicates allowed ArrayList, LinkedList, Vector
Set Unordered collection without duplicates HashSet, LinkedHashSet, TreeSet
Queue FIFO collection LinkedList, PriorityQueue
Deque Double-ended queue ArrayDeque, LinkedList
Map Collection of key-value pairs HashMap, TreeMap, LinkedHashMap
SortedSet Sorted set, maintains natural ordering or comparator order TreeSet
SortedMap Sorted map, maintains natural ordering or comparator order TreeMap

These interfaces provide a broad range of collection types that can be chosen based on the specific needs of your application, whether you need sorting, ordering, duplicate handling, or thread safety.

Top comments (0)