0% found this document useful (0 votes)
3 views4 pages

Java Collection Framework

The Java Collection Framework (JCF) is a set of classes and interfaces for reusable data structures like List, Set, Queue, and Map. It includes key interfaces such as Iterable and Collection, and provides implementations for various data structures, each with unique characteristics regarding duplicates and order. Additionally, utility classes like Collections offer methods for manipulating these data structures, and real-world applications demonstrate their practical use, such as in a Student Management System.

Uploaded by

Nitish sood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Java Collection Framework

The Java Collection Framework (JCF) is a set of classes and interfaces for reusable data structures like List, Set, Queue, and Map. It includes key interfaces such as Iterable and Collection, and provides implementations for various data structures, each with unique characteristics regarding duplicates and order. Additionally, utility classes like Collections offer methods for manipulating these data structures, and real-world applications demonstrate their practical use, such as in a Student Management System.

Uploaded by

Nitish sood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Collection Framework – Detailed Explanation

Java Collection Framework (JCF) is a set of classes and interfaces that implement commonly
reusable data structures. It provides ready-made implementations of data structures like List, Set,
Queue, Map etc.

1. Hierarchy of Collection Framework

Iterable

Collection Map

List Set Queue

2. Key Interfaces
Iterable – Root interface. Defines iterator().
Collection – Base interface for List, Set, Queue. Methods: add(), remove(), size(), clear(),
contains().

3. List (Allows duplicates, maintains order)


Implementations: ArrayList, LinkedList, Vector, Stack
import [Link].*;

public class ListExample {


public static void main(String[] args) {
List list = new ArrayList<>();
[Link]("Java");
[Link]("Python");
[Link]("C++");
[Link]("Java"); // allows duplicates
[Link]("List: " + list);

for(String lang : list) {


[Link](lang);
}
}
}

4. Set (Does NOT allow duplicates, order depends on


implementation)
Implementations: HashSet, LinkedHashSet, TreeSet
import [Link].*;

public class SetExample {


public static void main(String[] args) {
Set set = new HashSet<>();
[Link](20);
[Link](10);
[Link](30);
[Link](10); // duplicate ignored

[Link]("HashSet: " + set);

Set treeSet = new TreeSet<>(set);


[Link]("TreeSet (Sorted): " + treeSet);
}
}

5. Queue (FIFO – First In First Out)


Implementations: PriorityQueue, ArrayDeque
import [Link].*;

public class QueueExample {


public static void main(String[] args) {
Queue queue = new LinkedList<>();
[Link]("A");
[Link]("B");
[Link]("C");

[Link]("Queue: " + queue);


[Link]("Removed: " + [Link]());
[Link]("After poll: " + queue);
}
}

6. Map (Key–Value pair, keys unique, values can be duplicate)


Implementations: HashMap, LinkedHashMap, TreeMap, Hashtable
import [Link].*;

public class MapExample {


public static void main(String[] args) {
Map map = new HashMap<>();
[Link](101, "Deepak");
[Link](102, "Aman");
[Link](103, "Ravi");
[Link](101, "Karan"); // duplicate key overwrites value

[Link]("HashMap: " + map);

for([Link] entry : [Link]()) {


[Link]([Link]() + " -> " + [Link]());
}
}
}

7. Important Utility Classes


Collections (class) – provides static methods like sort(), reverse(), shuffle(), min(), max().
import [Link].*;

public class CollectionsExample {


public static void main(String[] args) {
List list = new ArrayList<>([Link](5, 2, 8, 1));

[Link](list);
[Link]("Sorted: " + list);

[Link](list);
[Link]("Reversed: " + list);

[Link]("Max: " + [Link](list));


}
}

8. Differences Summary

Interface | Allows Duplicates | Maintains Order | Example Classes


----------|------------------|----------------|----------------
List | Yes | Yes | ArrayList, LinkedList
Set | No | Depends | HashSet, TreeSet
Queue | Yes | FIFO | LinkedList, PriorityQueue
Map | Keys No, Values Yes | Depends | HashMap, TreeMap
9. Real-World Example: Student Management System
This example demonstrates how List, Set, and Map can be used together in a real-world
application.
- List: To store all students in order of registration
- Set: To maintain unique course names
- Map: To map student IDs to student details
import [Link].*;

class Student {
int id;
String name;
String course;

Student(int id, String name, String course) {


[Link] = id;
[Link] = name;
[Link] = course;
}

public String toString() {


return id + " - " + name + " (" + course + ")";
}
}

public class StudentManagement {


public static void main(String[] args) {
// List of students (order of registration)
List students = new ArrayList<>();
[Link](new Student(101, "Deepak", "Java"));
[Link](new Student(102, "Aman", "Python"));
[Link](new Student(103, "Ravi", "Java"));

// Set of unique courses


Set courses = new HashSet<>();
for(Student s : students) {
[Link]([Link]);
}

// Map student ID -> student details


Map studentMap = new HashMap<>();
for(Student s : students) {
[Link]([Link], s);
}

[Link]("All Students: " + students);


[Link]("Unique Courses: " + courses);
[Link]("Find Student with ID 102: " + [Link](102));
}
}

You might also like