Home RoadMap Blog Contact us Learn

4.Core Java Concepts: Collections Framework

Core Java Concepts: Collections Framework

The Java Collections Framework provides a set of classes and interfaces to store and manipulate groups of objects. It includes classes for dynamic arrays, sets, maps, queues, and iterators. Let's explore the key components.

1. List

A List is an ordered collection that allows duplicate elements. You can access elements by their index. Common implementations include ArrayList and LinkedList.

ArrayList

ArrayList is a resizable array implementation of the List interface. It allows random access to elements using indices.

import java.util.ArrayList;

ArrayList fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Mango");
System.out.println(fruits.get(1));  // Output: Banana

LinkedList

LinkedList is a doubly linked list implementation of the List and Deque interfaces. It is efficient for adding and removing elements at both ends.

import java.util.LinkedList;

LinkedList cars = new LinkedList<>();
cars.add("Toyota");
cars.add("Ford");
cars.addFirst("Tesla");
System.out.println(cars);  // Output: [Tesla, Toyota, Ford]

2. Set

A Set is a collection that does not allow duplicate elements. Implementations include HashSet, LinkedHashSet, and TreeSet.

HashSet

HashSet is backed by a hash table, and it does not guarantee any order of elements.

import java.util.HashSet;

HashSet countries = new HashSet<>();
countries.add("India");
countries.add("USA");
countries.add("India");  // Duplicate element
System.out.println(countries);  // Output: [India, USA] (No duplicates)

LinkedHashSet

LinkedHashSet maintains insertion order, meaning elements are stored in the order they are added.

import java.util.LinkedHashSet;

LinkedHashSet cities = new LinkedHashSet<>();
cities.add("New York");
cities.add("Los Angeles");
cities.add("Chicago");
System.out.println(cities);  // Output: [New York, Los Angeles, Chicago]

TreeSet

TreeSet stores elements in a sorted order (ascending by default). It is backed by a balanced tree (Red-Black tree).

import java.util.TreeSet;

TreeSet numbers = new TreeSet<>();
numbers.add(10);
numbers.add(5);
numbers.add(20);
System.out.println(numbers);  // Output: [5, 10, 20] (sorted)

3. Map

A Map is a collection of key-value pairs. Keys are unique, but values can be duplicated. Common implementations include HashMap, LinkedHashMap, and TreeMap.

HashMap

HashMap stores key-value pairs, with no guarantee of order.

import java.util.HashMap;

HashMap scores = new HashMap<>();
scores.put("Alice", 90);
scores.put("Bob", 85);
scores.put("Alice", 95);  // Overwrites previous value
System.out.println(scores);  // Output: {Bob=85, Alice=95}

LinkedHashMap

LinkedHashMap maintains insertion order for key-value pairs.

import java.util.LinkedHashMap;

LinkedHashMap ages = new LinkedHashMap<>();
ages.put("John", 30);
ages.put("Sara", 25);
System.out.println(ages);  // Output: {John=30, Sara=25}

TreeMap

TreeMap stores key-value pairs in sorted order based on the keys.

import java.util.TreeMap;

TreeMap population = new TreeMap<>();
population.put("New York", 8175133);
population.put("Los Angeles", 3792621);
System.out.println(population);  // Output: {Los Angeles=3792621, New York=8175133}

4. Queue

A Queue is a collection that follows the First-In-First-Out (FIFO) principle. Implementations include PriorityQueue and LinkedList.

PriorityQueue

PriorityQueue orders elements based on their priority (natural ordering or a custom comparator).

import java.util.PriorityQueue;

PriorityQueue queue = new PriorityQueue<>();
queue.add(5);
queue.add(10);
queue.add(1);
System.out.println(queue.poll());  // Output: 1 (smallest element)

LinkedList as a Queue

LinkedList can be used as a queue by utilizing its Deque methods, such as addLast() and pollFirst().

import java.util.LinkedList;

LinkedList tasks = new LinkedList<>();
tasks.add("Task 1");
tasks.add("Task 2");
System.out.println(tasks.poll());  // Output: Task 1

5. Iterators

Iterators are used to traverse through collections. There are two main types of iterators in Java:

Iterator

Iterator can be used to traverse through any collection. It allows removing elements during iteration.

import java.util.ArrayList;
import java.util.Iterator;

ArrayList animals = new ArrayList<>();
animals.add("Dog");
animals.add("Cat");

Iterator iterator = animals.iterator();
while (iterator.hasNext()) {
    System.out.println(iterator.next());
}

ListIterator

ListIterator allows bidirectional traversal of a List and can modify the list while iterating.

import java.util.LinkedList;
import java.util.ListIterator;

LinkedList books = new LinkedList<>();
books.add("Book 1");
books.add("Book 2");

ListIterator listIterator = books.listIterator();
while (listIterator.hasNext()) {
    System.out.println(listIterator.next());
}
Quick Exercise: Try creating a collection of your choice and traverse through it using an Iterator or ListIterator. Also, experiment with different types of collections like Set, Map, and Queue.

Conclusion

The Java Collections Framework is essential for managing groups of objects in an efficient way. Understanding how to use Lists, Sets, Maps, Queues, and Iterators can help you handle data structures effectively in automation scripts.

Collections Framework in Java

Recent Posts