Java Collection Framework (JCF)
1. Introduction
The Java Collection Framework is a unified architecture for storing and manipulating
groups of objects. It provides:
Interfaces (blueprints)
Classes (implementations)
Algorithms (methods for operations like sorting/searching)
Available in package: [Link]
Key Components of JCF
1. Interfaces
Define the structure of collections:
Collection
List
Set
Queue
Map (separate hierarchy)
2. Classes
Concrete implementations:
ArrayList, LinkedList
HashSet, TreeSet
HashMap, TreeMap
3. Algorithms
Methods like:
Sorting ([Link]())
Searching
Reversing
3. Collection Hierarchy
Iterable
|
Collection
|-----------------------------|
| | |
List Set Queue
Separate hierarchy:
Map (Key-Value Pair)
4. Iterable Interface
Root interface of collection framework
Contains:
Iterator iterator();
5. Collection Interface
Base interface for all collections
Important methods:
add()
remove()
size()
clear()
contains()
isEmpty()
6. List Interface
Features:
Ordered collection
Allows duplicates
Indexed access
Classes:
1. ArrayList
Dynamic array
Fast retrieval
Slow insertion/deletion (middle)
ArrayList<Integer> list = new ArrayList<>();
[Link](10);
2. LinkedList
Doubly linked list
Fast insertion/deletion
Slow access
LinkedList<Integer> list = new LinkedList<>();
3. Vector
Thread-safe (synchronized)
Legacy class
7. Set Interface
Features:
No duplicate elements
Unordered (mostly)
Classes:
1. HashSet
No duplicates
No order
HashSet<Integer> set = new HashSet<>();
2. LinkedHashSet
Maintains insertion order
3. TreeSet
Sorted (ascending order)
Uses Red-Black Tree
8. Queue Interface
Features:
FIFO (First In First Out)
Classes:
1. PriorityQueue
Elements sorted by priority
PriorityQueue<Integer> pq = new PriorityQueue<>();
2. Deque (Double-ended queue)
Insert/delete from both ends
Deque<Integer> dq = new ArrayDeque<>();
9. Map Interface (Separate Hierarchy)
Features:
Stores key-value pairs
Keys are unique
Classes:
1. HashMap
Fast access
No order
HashMap<Integer, String> map = new HashMap<>();
2. LinkedHashMap
Maintains insertion order
3. TreeMap
Sorted keys
4. Hashtable
Thread-safe (legacy)
10. Iterator Interface
Used to traverse elements.
Iterator<Integer> it = [Link]();
while([Link]()) {
[Link]([Link]());
}
11. Comparable vs Comparator
Comparable
Used for natural sorting
compareTo()
Comparator
Custom sorting
compare()
12. Collections Class (Utility Class)
Methods:
[Link](list);
[Link](list);
[Link](list);
[Link](list);
13. Differences
List vs Set
Feature List Set
Duplicates Allowed Not allowed
Order Maintained Not guaranteed
ArrayList vs LinkedList
Feature ArrayList LinkedList
Access Fast Slow
Insert/Delete Slow Fast
HashMap vs TreeMap
Feature HashMap TreeMap
Order No Sorted
Performance Fast Slower
14. Advantages of JCF
Reduces programming effort
Increases performance
Provides reusable data structures
Supports algorithms
JAVA COLLECTION FRAMEWORK — WITH CODE
1. Collection Interface (Common Methods)
import [Link].*;
public class CollectionDemo {
public static void main(String[] args) {
Collection<Integer> c = new ArrayList<>();
[Link](10); // add()
[Link](20);
[Link]([Link]()); // size()
[Link]([Link](10)); // contains()
[Link](10); // remove()
[Link]([Link]()); // isEmpty()
[Link](); // clear()
[Link](c); // []
}
}
2. List Interface
All Important Methods
import [Link].*;
public class ListMethods {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
[Link](10); // add()
[Link](20);
[Link](1, 15); // add(index, element)
[Link]([Link](1)); // get()
[Link](1, 100); // set()
[Link]([Link](20)); // indexOf()
[Link]([Link](20)); // lastIndexOf()
[Link](1); // remove(index)
[Link](list);
}
}
ArrayList
import [Link].*;
public class ArrayListDemo {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
[Link]("A");
[Link]("B");
[Link]("C");
[Link](list);
// Traversal
for(String s : list){
[Link](s);
}
}
}
LinkedList
import [Link].*;
public class LinkedListDemo {
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<>();
[Link](10);
[Link](5); // specific method
[Link](20);
[Link]();
[Link]();
[Link](list);
}
}
Vector
import [Link].*;
public class VectorDemo {
public static void main(String[] args) {
Vector<Integer> v = new Vector<>();
[Link](10);
[Link](20);
[Link](v);
}
}
3. Set Interface
HashSet
import [Link].*;
public class HashSetDemo {
public static void main(String[] args) {
HashSet<Integer> set = new HashSet<>();
[Link](10);
[Link](20);
[Link](10); // duplicate ignored
[Link](set);
[Link](20);
[Link]([Link](10));
}
}
LinkedHashSet
import [Link].*;
public class LinkedHashSetDemo {
public static void main(String[] args) {
LinkedHashSet<Integer> set = new LinkedHashSet<>();
[Link](30);
[Link](10);
[Link](20);
[Link](set); // maintains order
}
}
TreeSet
import [Link].*;
public class TreeSetDemo {
public static void main(String[] args) {
TreeSet<Integer> set = new TreeSet<>();
[Link](30);
[Link](10);
[Link](20);
[Link](set); // sorted
}
}
4. Queue Interface
Methods Demo
import [Link].*;
public class QueueMethods {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<>();
[Link](10); // add()
[Link](20); // offer()
[Link]([Link]()); // element()
[Link]([Link]()); // peek()
[Link](); // remove()
[Link](); // poll()
[Link](q);
}
}
PriorityQueue
import [Link].*;
public class PriorityQueueDemo {
public static void main(String[] args) {
PriorityQueue<Integer> pq = new PriorityQueue<>();
[Link](30);
[Link](10);
[Link](20);
[Link](pq); // heap order
while(![Link]()){
[Link]([Link]()); // sorted output
}
}
}
Deque (ArrayDeque)
import [Link].*;
public class DequeDemo {
public static void main(String[] args) {
Deque<Integer> dq = new ArrayDeque<>();
[Link](10);
[Link](20);
[Link]([Link]());
[Link]([Link]());
[Link]();
[Link]();
[Link](dq);
}
}
5. Map Interface
Methods Demo
import [Link].*;
public class MapMethods {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
[Link](1, "A"); // put()
[Link](2, "B");
[Link]([Link](1)); // get()
[Link](2); // remove()
[Link]([Link](1));
[Link]([Link]("A"));
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
}
}
HashMap
import [Link].*;
public class HashMapDemo {
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<>();
[Link](1, "Java");
[Link](2, "Python");
[Link](map);
}
}
LinkedHashMap
import [Link].*;
public class LinkedHashMapDemo {
public static void main(String[] args) {
LinkedHashMap<Integer, String> map = new LinkedHashMap<>();
[Link](3, "C");
[Link](1, "Java");
[Link](2, "Python");
[Link](map); // insertion order
}
}
TreeMap
import [Link].*;
public class TreeMapDemo {
public static void main(String[] args) {
TreeMap<Integer, String> map = new TreeMap<>();
[Link](3, "C");
[Link](1, "Java");
[Link](2, "Python");
[Link](map); // sorted keys
}
}
6. Iterator
import [Link].*;
public class IteratorDemo {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
[Link](10);
[Link](20);
Iterator<Integer> it = [Link]();
while([Link]()){
[Link]([Link]());
}
}
}
7. Comparable
class Student implements Comparable<Student> {
int marks;
Student(int marks){
[Link] = marks;
}
public int compareTo(Student s){
return [Link] - [Link];
}
}
8. Comparator
import [Link].*;
class SortByMarks implements Comparator<Student> {
public int compare(Student a, Student b){
return [Link] - [Link];
}
}
9. Collections Class
import [Link].*;
public class CollectionsDemo {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
[Link](30);
[Link](10);
[Link](20);
[Link](list); // sort
[Link](list); // reverse
[Link]([Link](list)); // max
[Link]([Link](list)); // min
[Link](list);
}
}