📘LESSON 1
3.1 Lesson Outcomes
After completing this lesson, learners will be able to:
- Explain the need for Java Collections.
- Describe the Java Collection Framework hierarchy.
- Explain List, Set, Queue, and Map interfaces.
- Differentiate between ArrayList, LinkedList, Vector, HashMap, LinkedHashMap, and TreeMap.
- Perform collection operations such as add, remove, update, sorting, and iteration.
- Explain basic data structures used in Java Collections.
- Use collection interfaces and implementations in Java applications.
3.2 Overview
Modern software systems process and manage large amounts of data that require flexible and efficient storage structures. Java Collections provide reusable data structures and interfaces that simplify data management and improve application performance.
This lesson introduces learners to Java Collections and explains collection interfaces, implementations, sorting mechanisms, data structures, and collection operations used in Java applications.
Java Collections are widely used in:
- enterprise systems,
- banking applications,
- healthcare systems,
- inventory systems,
- and cloud platforms.
Understanding Java Collections is important because collections form the foundation of data management in modern Java programming.
KT0101 — Need for Collections
Collections are used to store and manage groups of objects dynamically.
Arrays have limitations because:
- they have fixed sizes,
- resizing is difficult,
- and data management is less flexible.
Why Collections are Needed
Collections support:
- dynamic resizing,
- flexible storage,
- searching,
- sorting,
- and efficient data processing.
Importance of Collections
Collections improve:
- scalability,
- maintainability,
- and data organization.
KT0102 — Java Collection Framework Hierarchy
The Java Collection Framework provides interfaces and classes for storing and managing data.
Collection Framework Hierarchy
Collection
|
--------------------------------
| | |
List Set Queue
Common Collection Implementations
| Interface | Implementations |
|---|---|
| List | ArrayList, LinkedList, Vector |
| Set | HashSet, LinkedHashSet, TreeSet |
| Queue | PriorityQueue |
| Map | HashMap, TreeMap |
Importance of Collection Framework
The Collection Framework supports:
- reusable data structures,
- standardized operations,
- and efficient programming.
KT0103 — List Interface
The List interface stores elements in a specific order.
Lists:
- preserve insertion order,
- allow duplicates,
- and support indexed access.
Position/Order is Advisable
Lists maintain the order in which elements are inserted.
Example
ArrayList<String> names =
new ArrayList<String>();
names.add("John");
names.add("Sarah");
Immutability and Introduction of Implementations – Array
Arrays have fixed sizes and limited flexibility.
Collections improve flexibility compared to arrays.
Implementations — ArrayList vs LinkedList
| ArrayList | LinkedList |
|---|---|
| Faster retrieval | Faster insertion/removal |
| Dynamic array | Linked nodes |
Implementations — ArrayList vs Vector
| ArrayList | Vector |
|---|---|
| Not synchronized | Synchronized |
| Faster | Thread-safe |
Methods to Add, Remove and Change Elements and Lists
Adding Elements
names.add("David");
Removing Elements
names.remove("John");
Changing Elements
names.set(0, "Peter");
KT0104 — List and ArrayList
ArrayList is a commonly used implementation of the List interface.
ArrayList dynamically resizes during execution.
Iterating Around Elements
Collections can be traversed using loops.
for Loop Example
for(int i = 0; i < names.size(); i++) {
System.out.println(names.get(i));
}
Enhanced for Loop Example
for(String name : names) {
System.out.println(name);
}
Choosing Iteration Approach for Printing and Display
| Approach | Purpose |
|---|---|
| for loop | Indexed access |
| enhanced loop | Simple traversal |
Type Safety and Removing Integers
Generics improve type safety by restricting collection types.
Example
ArrayList<String> cities =
new ArrayList<String>();
Sorting – Introduction to Collections sort Static
Java provides:
Collections.sort()
for sorting collection elements.
Example
Collections.sort(numbers);
Sorting – Implementing Comparable Interface
The Comparable interface allows custom sorting logic.
Example
class Student implements Comparable<Student> {
int age;
public int compareTo(Student s) {
return this.age - s.age;
}
}
Importance of Sorting
Sorting improves:
- searching,
- reporting,
- and organized data management.
KT0105 — Data Structures
Data structures organize and store data efficiently.
Array
Arrays store elements in contiguous memory locations.
LinkedList
LinkedList stores elements as linked nodes.
Hashing
Hashing stores and retrieves data using hash values.
Tree – Sorted Order
Tree structures automatically maintain sorted order.
Importance of Data Structures
Data structures improve:
- storage efficiency,
- searching,
- and performance.
KT0106 — Set Interface
The Set interface stores unique elements only.
Sets do not allow duplicates.
Hands-on — HashSet, LinkedHashSet and TreeSet
| Set Type | Characteristic |
|---|---|
| HashSet | Unordered |
| LinkedHashSet | Maintains insertion order |
| TreeSet | Sorted order |
Find Unique Characters in a List
Sets can remove duplicate values automatically.
Example
HashSet<Character> letters =
new HashSet<Character>();
TreeSet – Methods from NavigableSet
| Method | Purpose |
|---|---|
| floor() | Greatest value ≤ given value |
| lower() | Greatest value < given value |
| upper() | Smallest value > given value |
| subSet() | Returns subset |
| headSet() | Returns head portion |
Queue Interface – Process Elements in Order
Queues process elements using:
First In First Out (FIFO)
Importance of Sets and Queues
Sets and queues improve:
- uniqueness handling,
- ordered processing,
- and data management.
KT0107 — Introduction to PriorityQueue – Basic Methods and Customized Priority
PriorityQueue processes elements based on priority rather than insertion order.
Basic PriorityQueue Methods
| Method | Purpose |
|---|---|
| add() | Inserts element |
| poll() | Removes highest priority |
| peek() | Retrieves highest priority |
Example
PriorityQueue<Integer> queue =
new PriorityQueue<Integer>();
Customized Priority
PriorityQueue supports custom ordering using comparators.
Importance of PriorityQueue
PriorityQueue supports:
- scheduling,
- priority processing,
- and task management.
KT0108 — Map Interface
The Map interface stores:
- key-value pairs.
Maps do not allow duplicate keys.
Key and Value
| Component | Purpose |
|---|---|
| Key | Unique identifier |
| Value | Associated data |
Implementations — HashMap, HashTable, LinkedHashMap
| Implementation | Characteristic |
|---|---|
| HashMap | Fast performance |
| Hashtable | Synchronized |
| LinkedHashMap | Maintains insertion order |
Basic Operations
Adding Values
map.put("A", 100);
Retrieving Values
map.get("A");
Comparison — HashMap vs LinkedHashMap vs TreeMap
| Map Type | Characteristic |
|---|---|
| HashMap | Unordered |
| LinkedHashMap | Ordered |
| TreeMap | Sorted |
Count Occurrences of Characters and Words
Maps are commonly used for counting frequencies.
Example
HashMap<String, Integer> words =
new HashMap<String, Integer>();
TreeMap – Methods from NavigableMap
| Method | Purpose |
|---|---|
| floorKey() | Greatest key ≤ given key |
| higherKey() | Smallest key > given key |
| firstEntry() | Returns first key-value pair |
Importance of Maps
Maps support:
- fast retrieval,
- key-value management,
- and efficient searching.
3.5 Key Notes / Summary
- Collections store and manage groups of objects dynamically.
- The Collection Framework contains List, Set, Queue, and Map interfaces.
- ArrayList, LinkedList, and Vector are List implementations.
- Sets store unique elements.
- Queues process elements in order.
- Maps store key-value pairs.
- Collections support sorting, iteration, and efficient data management.