What is the Java Collections Framework?
The Java Collections Framework provides architectures to store and manipulate groups of objects. It includes interfaces like List, Set, and Map.
1. List Interface (Ordered, Allows Duplicates)
import java.util.*;
public class ListExample {
public static void main(String[] args) {
// ArrayList - dynamic array
List arrayList = new ArrayList<>();
arrayList.add("Apple");
arrayList.add("Banana");
arrayList.add("Apple"); // Duplicate allowed
System.out.println("ArrayList: " + arrayList);
// Access by index - O(1)
System.out.println("Element at index 1: " + arrayList.get(1));
// LinkedList - doubly linked list
List linkedList = new LinkedList<>();
linkedList.add("First");
linkedList.add("Second");
linkedList.add(1, "Inserted");
System.out.println("LinkedList: " + linkedList);
// Iteration
for (String item : arrayList) {
System.out.println(item);
}
}
}
2. Set Interface (Unordered, No Duplicates)
public class SetExample {
public static void main(String[] args) {
// HashSet - hash table, no order
Set hashSet = new HashSet<>();
hashSet.add("Java");
hashSet.add("Python");
hashSet.add("Java"); // Duplicate - will not be added
System.out.println("HashSet: " + hashSet);
// TreeSet - sorted order
Set treeSet = new TreeSet<>();
treeSet.add("Banana");
treeSet.add("Apple");
treeSet.add("Cherry");
System.out.println("TreeSet (sorted): " + treeSet);
// LinkedHashSet - insertion order
Set linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add("One");
linkedHashSet.add("Two");
linkedHashSet.add("Three");
System.out.println("LinkedHashSet: " + linkedHashSet);
// Check existence - O(1)
System.out.println("Contains Java? " + hashSet.contains("Java"));
}
}
3. Map Interface (Key-Value Pairs)
public class MapExample {
public static void main(String[] args) {
// HashMap - no order
Map hashMap = new HashMap<>();
hashMap.put("Apple", 10);
hashMap.put("Banana", 20);
hashMap.put("Orange", 15);
// Iterate
for (Map.Entry entry : hashMap.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
// Get value
System.out.println("Value for Apple: " + hashMap.get("Apple"));
// TreeMap - sorted by keys
Map treeMap = new TreeMap<>();
treeMap.put("Banana", 20);
treeMap.put("Apple", 10);
treeMap.put("Cherry", 30);
System.out.println("TreeMap (sorted): " + treeMap);
// Check if key exists
System.out.println("Contains key Banana? " + hashMap.containsKey("Banana"));
}
}
Collection Hierarchy
/*
Iterable (Interface)
└── Collection (Interface)
├── List (Interface)
│ ├── ArrayList
│ ├── LinkedList
│ └── Vector
└── Set (Interface)
├── HashSet
├── LinkedHashSet
└── TreeSet
Map (Interface)
├── HashMap
├── LinkedHashMap
├── TreeMap
└── Hashtable
*/
Practical Example - Student Management
class Student {
int id;
String name;
Student(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return id + ": " + name;
}
}
public class StudentManagement {
public static void main(String[] args) {
// List - store all students
List students = new ArrayList<>();
students.add(new Student(1, "Alice"));
students.add(new Student(2, "Bob"));
students.add(new Student(3, "Charlie"));
// Set - unique course names
Set courses = new HashSet<>();
courses.add("Java");
courses.add("Python");
courses.add("Java"); // Won't add duplicate
// Map - student id to student object
Map studentMap = new HashMap<>();
for (Student s : students) {
studentMap.put(s.id, s);
}
// Retrieve by id
Student found = studentMap.get(2);
System.out.println("Found student: " + found);
}
}
Learn Java Collections Framework with Online Learner!
0
likes
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
