Spex3
    

Java Collections Framework


    

    

Java Collections Framework
The Java Collections Framework (JCF) provides a set of classes and interfaces for storing and manipulating groups of objects efficiently.

1. Overview of Collections Framework
✅ Key Interfaces in Java Collections

Interface Description Implementations
List Ordered collection (allows duplicates) ArrayList, LinkedList
Set Unordered collection (no duplicates) HashSet, TreeSet
Map Key-value pairs HashMap, TreeMap

2. Lists in Java
A List allows duplicate elements and maintains insertion order.

2.1 ArrayList (Fast, dynamic array)
✅ Example using ArrayList


import java.util.ArrayList;

public class ArrayListExample {
public static void main(String[] args) {
ArrayList names = new ArrayList<>();

// Adding elements
names.add("Alice");
names.add("Bob");
names.add("Charlie");

// Accessing elements
System.out.println(names.get(1)); // Output: Bob

// Removing an element
names.remove("Alice");

// Iterating through list
for (String name : names) {
System.out.println(name);
}
}
}


💡 Output:


Bob
Bob
Charlie


2.2 LinkedList (Doubly linked list, better for frequent insertions/deletions)
✅ Example using LinkedList


import java.util.LinkedList;

public class LinkedListExample {
public static void main(String[] args) {
LinkedList numbers = new LinkedList<>();

// Adding elements
numbers.add(10);
numbers.add(20);
numbers.addFirst(5); // Add at the beginning

// Removing an element
numbers.removeLast(); // Removes last element

// Iterating through list
for (int num : numbers) {
System.out.println(num);
}
}
}


💡 Output:

5
10

3. Sets in Java
A Set does not allow duplicates and does not maintain insertion order (except for TreeSet).

3.1 HashSet (Unordered, fast lookups)
✅ Example using HashSet


import java.util.HashSet;

public class HashSetExample {
public static void main(String[] args) {
HashSet colors = new HashSet<>();

colors.add("Red");
colors.add("Green");
colors.add("Blue");
colors.add("Red"); // Duplicate, will not be added

// Iterating through set
for (String color : colors) {
System.out.println(color);
}
}
}


💡 Output (Order may vary):

Red
Blue
Green

3.2 TreeSet (Sorted order)
✅ Example using TreeSet


import java.util.TreeSet;

public class TreeSetExample {
public static void main(String[] args) {
TreeSet numbers = new TreeSet<>();

numbers.add(50);
numbers.add(20);
numbers.add(30);
numbers.add(10);

// Displaying elements in sorted order
for (int num : numbers) {
System.out.println(num);
}
}
}


💡 Output:


10
20
30
50

4. Maps in Java
A Map stores key-value pairs, where each key is unique.

4.1 HashMap (Unordered key-value store)

✅ Example using HashMap


import java.util.HashMap;

public class HashMapExample {
public static void main(String[] args) {
HashMap studentMarks = new HashMap<>();

// Adding key-value pairs
studentMarks.put("Alice", 85);
studentMarks.put("Bob", 90);
studentMarks.put("Charlie", 78);

// Accessing values
System.out.println("Bob's marks: " + studentMarks.get("Bob"));

// Iterating through map
for (String key : studentMarks.keySet()) {
System.out.println(key + ": " + studentMarks.get(key));
}
}
}


💡 Output:


Bob's marks: 90
Alice: 85
Bob: 90
Charlie: 78
4.2 TreeMap (Sorted keys)

✅ Example using TreeMap


import java.util.TreeMap;

public class TreeMapExample {
public static void main(String[] args) {
TreeMap students = new TreeMap<>();

// Adding key-value pairs
students.put(102, "Alice");
students.put(101, "Bob");
students.put(103, "Charlie");

// Iterating through sorted keys
for (int key : students.keySet()) {
System.out.println(key + ": " + students.get(key));
}
}
}


💡 Output:

101: Bob
102: Alice
103: Charlie


5. Iterators in Java
An Iterator is used to traverse through a collection.

✅ Example using Iterator


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

public class IteratorExample {
public static void main(String[] args) {
ArrayList names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
names.add("Charlie");

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


💡 Output:


Alice
Bob
Charlie

Summary

Collection Type Description Key Features

ArrayList Resizable array Fast lookup, slow insertion

LinkedList Doubly linked list Fast insertion, slow lookup

HashSet Unordered set No duplicates, fast lookup

TreeSet Sorted set No duplicates, sorted order

HashMap Key-value pair Unordered, fast lookup

TreeMap Sorted key-value pair Sorted keys


    Date: 2025-03-28 00:00:00.000000