3.1 Lesson Outcomes
After completing this lesson, learners will be able to:
- Create arrays and ArrayLists in Java.
- Search and sort collection data.
- Use TreeSet collections.
- Store and retrieve information using HashMap.
- Display collection data in structured formats.
3.2 Overview
Java collections provide flexible data structures used to store, organize, search, and manage data efficiently. The Java Collections Framework includes:
- ArrayList,
- TreeSet,
- HashMap,
- and other collection classes.
Collections are important in:
- school management systems,
- banking applications,
- inventory systems,
- and enterprise software.
This lesson introduces learners to practical collection management using Java programming.
Understanding Java collections is important because collections simplify data storage, searching, sorting, and retrieval operations.
PA0101 — Create a List, ArrayList, Search and Sort
Java collections can store and organize large amounts of information dynamically.
Java Example:
import java.util.*;
public class StudentArrayList {
public static void main(String[] args) {
ArrayList students = new ArrayList<>();
students.add(“Alice – Grade 10 – 85”);
students.add(“Bob – Grade 11 – 92”);
students.add(“Charlie – Grade 10 – 78”);
students.add(“David – Grade 12 – 95”);
System.out.println(“Grade 10 Students:”);
for (String s : students) {
if (s.contains(“Grade 10”)) {
System.out.println(s);
}
}
Collections.sort(students);
System.out.println(”\nSorted Alphabetically:”);
for (String s : students) {
System.out.println(s);
}
}
}
Practical Activity
Learners must:
- create arrays and ArrayLists,
- search for specific information,
- sort data alphabetically,
- sort data numerically,
- and display structured outputs.
PA0102 — Use TreeSet to Find Learner with Highest Marks
TreeSet stores sorted unique values automatically.
Java Example:
import java.util.*;
class Student implements Comparable {
String name;
int marks;
Student(String name, int marks) {
this.name = name;
this.marks = marks;
}
@Override
public int compareTo(Student other) {
return this.marks – other.marks;
}
@Override
public String toString() {
return name + “ scored “ + marks;
}
}
public class TreeSetExample {
public static void main(String[] args) {
TreeSet students = new TreeSet<>();
students.add(new Student(“Alice”, 85));
students.add(new Student(“Bob”, 92));
students.add(new Student(“Charlie”, 78));
students.add(new Student(“David”, 95));
Student topStudent = students.last();
System.out.println(“Top Student: “ + topStudent);
}
}
Practical Activity
Learners must:
- create TreeSet collections,
- compare student objects,
- sort collection data,
- and retrieve highest values.
PA0103 — Use ArrayList with Map Information
HashMap stores information using key-value pairs.
Java Example:
import java.util.*;
public class StudentMapExample {
public static void main(String[] args) {
Map<Integer, String> studentMap = new HashMap<>();
studentMap.put(101, “Alice – Grade 10 – 85 – 0712345678”);
studentMap.put(102, “Bob – Grade 11 – 92 – 0723456789”);
studentMap.put(103, “Charlie – Grade 10 – 78 – 0734567890”);
System.out.println(“Student with ID 102: “ + studentMap.get(102));
System.out.println(”\nAll Students:”);
for (Map.Entry<Integer, String> entry : studentMap.entrySet()) {
System.out.println(“ID: “ + entry.getKey() + “, Info: “ + entry.getValue());
}
}
}
Practical Activity
Learners must:
- create HashMaps,
- store key-value data,
- retrieve information using keys,
- and display collection information.
AK0101 — Functions and Uses of Java Collections
Java Collection — Function
ArrayList — Dynamic arrays and flexible storage
TreeSet — Sorted unique data storage
HashMap — Key-value pair storage
Collections Framework — Searching and sorting support
Practical Activity
Learners must:
- identify collection types,
- explain collection functionality,
- and apply suitable collection structures.
IAC0101 — Expected Results with Collections in Java are Achieved
Expected Results
- Arrays and collections are created correctly.
- Data sorting works successfully.
- Searching operations display correct information.
- TreeSet retrieves highest scoring learners.
- HashMap stores and retrieves unique key-value data.
- Output is displayed clearly and correctly.
3.5 Key Notes / Summary
- ArrayLists store dynamic collections of data.
- TreeSet stores sorted unique elements.
- HashMap stores key-value information.
- Collections Framework supports sorting and searching.
- Collections improve data management and organization.
- Java collections simplify software development.