Program 10 : Collection Framework
Develop a Java program to demonstrate the Collections Framework, including the use of ArrayList and LinkedList. Showcase how these collections are used and manipulated.
ArrayList
Demonstration
java
import java.util.ArrayList;
public class ArrayListDemo {
public static void main(String[] args) {
System.out.println("--- Demonstrating ArrayList ---");
// 1. Creating an ArrayList of Strings
ArrayList<String> planets = new ArrayList<>();
// 2. Adding elements
planets.add("Mars"); // Add to the end
planets.add("Jupiter");
planets.add(0, "Earth"); // Add at a specific index
planets.add("Saturn");
System.out.println("Initial list of planets: " + planets);
// 3. Accessing elements
String thirdPlanet = planets.get(2); // Access by index (fast)
System.out.println("Element at index 2: " + thirdPlanet);
int marsIndex = planets.indexOf("Mars");
System.out.println("The index of 'Mars' is: " + marsIndex);
// 4. Updating an element
planets.set(0, "Planet Earth"); // Replace the element at index 0
System.out.println("Updated list: " + planets);
// 5. Removing elements
planets.remove("Jupiter"); // Remove by object value
planets.remove(1); // Remove by index ("Mars")
System.out.println("List after removals: " + planets);
// 6. Checking size and content
System.out.println("Current size of the list: " + planets.size());
System.out.println("Does the list contain 'Saturn'? " + planets.contains("Saturn"));
// 7. Iterating through the list
System.out.println("\n--- Iterating through the final list ---");
for (String planet : planets) {
System.out.println("- " + planet);
}
// 8. Clearing the list
planets.clear();
System.out.println("\nIs the list empty after clearing? " + planets.isEmpty());
}
}
--- Demonstrating ArrayList ---
Initial list of planets: [Earth, Mars, Jupiter, Saturn]
Element at index 2: Jupiter
The index of 'Mars' is: 1
Updated list: [Planet Earth, Mars, Jupiter, Saturn]
List after removals: [Planet Earth, Saturn]
Current size of the list: 2
Does the list contain 'Saturn'? true
--- Iterating through the final list ---
- Planet Earth
- Saturn
Is the list empty after clearing? true
LinkedList
Demonstration
This program focuses on LinkedList
, demonstrating its capabilities both as a standard List
and as a Deque
(double-ended queue), with methods for adding, removing, and examining elements at both ends.
java
import java.util.LinkedList;
public class LinkedListDemo {
public static void main(String[] args) {
System.out.println("--- Demonstrating LinkedList ---");
// 1. Creating a LinkedList of Integers
LinkedList<Integer> numbers = new LinkedList<>();
// 2. Adding elements (as a Deque)
numbers.addFirst(10); // Add to the front
numbers.addLast(30); // Add to the end
numbers.add(1, 20); // Add at a specific index (as a List)
System.out.println("Initial list: " + numbers);
// 3. Examining elements (as a Deque)
int first = numbers.peekFirst(); // Look at the first element
int last = numbers.peekLast(); // Look at the last element
System.out.println("First element (peek): " + first);
System.out.println("Last element (peek): " + last);
// 4. Removing elements (as a Deque)
int removedFirst = numbers.removeFirst(); // Remove from the front
int removedLast = numbers.removeLast(); // Remove from the end
System.out.println("Removed first element: " + removedFirst);
System.out.println("Removed last element: " + removedLast);
System.out.println("List after removals: " + numbers);
// 5. Using it as a Stack (LIFO - Last-In, First-Out)
System.out.println("\n--- Using LinkedList as a Stack ---");
numbers.push(100); // push() is equivalent to addFirst()
numbers.push(200);
System.out.println("After pushing 100 and 200: " + numbers);
int popped = numbers.pop(); // pop() is equivalent to removeFirst()
System.out.println("Popped element: " + popped);
System.out.println("Contains 20: " + numbers.contains(20));
System.out.println("Final list: " + numbers);
}
}
--- Demonstrating LinkedList ---
Initial list: [10, 20, 30]
First element (peek): 10
Last element (peek): 30
Removed first element: 10
Removed last element: 30
List after removals: [20]
--- Using LinkedList as a Stack ---
After pushing 100 and 200: [200, 100, 20]
Popped element: 200
Final list: [100, 20]
Combined Program
java
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class CollectionsDemo {
public static void main(String[] args) {
ArrayListDemo o1 = new ArrayListDemo();
o1.startDemo();
LinkedListDemo o2 = new LinkedListDemo();
o2.startDemo();
}
static class LinkedListDemo {
void startDemo() {
System.out.println("\n--- Demonstrating LinkedList ---");
List<Integer> numberQueue = new LinkedList<>();
// Using LinkedList as a Queue: adding elements to the end
numberQueue.addLast(10);
numberQueue.addLast(20);
numberQueue.addLast(30);
System.out.println("Initial LinkedList (as a queue): "
+ numberQueue);
// Adding an element to the beginning
numberQueue.addFirst(5);
System.out.println("LinkedList after adding 5 to the front: "
+ numberQueue);
// Removing an element from the front (FIFO - First-In, First-Out)
int removedNumber = numberQueue.removeFirst();
System.out.println("Removed the first element: "
+ removedNumber);
System.out.println("LinkedList after removal: "
+ numberQueue);
// --- Iterating Through the LinkedList ---
System.out.println("\n--- Iterating through the LinkedList ---");
for (Integer number : numberQueue) {
System.out.println("Number: " + number);
}
}
}
static class ArrayListDemo {
void startDemo() {
System.out.println("--- Demonstrating ArrayList ---");
// ArrayList of Strings
List<String> fruitList = new ArrayList<>();
// Adding elements to the ArrayList
fruitList.add("Apple");
fruitList.add("Banana");
fruitList.add("Cherry");
System.out.println("Initial ArrayList: "
+ fruitList);
// Accessing an element by index (fast operation)
String secondFruit = fruitList.get(1);
System.out.println("Element at index 1: "
+ secondFruit);
// Removing an element by index
fruitList.remove(0); // Removes "Apple"
System.out.println("ArrayList after removing element at 0: "
+ fruitList);
// Checking if an element exists
boolean hasBanana = fruitList.contains("Banana");
System.out.println("Does the list contain 'Banana'? "
+ hasBanana);
// --- Iterating Through Collection ---
System.out.println("\n--- Iterating through the ArrayList ---");
// Add one more fruit to iterate over
fruitList.add("Date");
for (String fruit : fruitList) {
System.out.println("Fruit: " + fruit);
}
}
}
}
Output
--- Demonstrating ArrayList ---
Initial ArrayList: [Apple, Banana, Cherry]
Element at index 1: Banana
ArrayList after removing element at index 0: [Banana, Cherry]
Does the list contain 'Banana'? true
--- Iterating through the ArrayList ---
Fruit: Banana
Fruit: Cherry
Fruit: Date
--- Demonstrating LinkedList ---
Initial LinkedList (as a queue): [10, 20, 30]
LinkedList after adding 5 to the front: [5, 10, 20, 30]
Removed the first element: 5
LinkedList after removal: [10, 20, 30]
--- Iterating through the LinkedList ---
Number: 10
Number: 20
Number: 30
Program Explanation
Collections Framework: The Java Collections Framework provides a set of interfaces and classes to represent and manage collections of objects. This program will focus on two key implementations of the
List
interface:ArrayList
andLinkedList
.ArrayList:
- It is a resizable array implementation of the
List
interface. - It provides fast random access to elements because it is backed by an array.
- Adding or removing elements from the middle of an
ArrayList
can be slow because it requires shifting subsequent elements. - In the program, we demonstrate creating an
ArrayList
, adding elements, accessing an element by its index, and removing an element.
- It is a resizable array implementation of the
LinkedList:
- It is a doubly-linked list implementation of the
List
andDeque
interfaces. - It is faster for adding and removing elements from the beginning or end of the list.
- Random access is slower than in an
ArrayList
because it requires traversing the list from the beginning or end. - The program shows how to create a
LinkedList
, add elements to the start and end (addFirst
,addLast
), and remove elements from the start (removeFirst
), demonstrating its use as a queue.
- It is a doubly-linked list implementation of the
Common Operations:
- The program also showcases common operations applicable to both
ArrayList
andLinkedList
, such as checking for an element's existence (contains
) and iterating through the elements using afor-each
loop.
- The program also showcases common operations applicable to both