============================================================ IT6003 - Advanced Java Programming Lab Session 1: Java Collections Framework State University of Zanzibar (SUZA) ============================================================ OBJECTIVES: - Understand and use List, Set, and Map interfaces - Choose appropriate collection types for different scenarios - Use Iterator for safe traversal and modification - Apply Collections utility methods PREREQUISITES: - Basic Java: arrays, classes, objects - Understanding of interfaces and generics basics ============================================================ PART A: ArrayList and LinkedList [30 minutes] ============================================================ Exercise 1: Student Roster -------------------------- Create a class Student with fields: regNo (String), name (String), gpa (double). Implement Comparable to sort by name alphabetically. a) Create an ArrayList and add 8 students with sample data. b) Print all students using enhanced for-each loop. c) Sort the list using Collections.sort() (natural ordering by name). d) Sort by GPA descending using a Comparator (lambda expression). e) Find and print the student with the highest GPA using Collections.max(). f) Remove all students with GPA below 2.0 using Iterator. Exercise 2: LinkedList as Queue ------------------------------- a) Create a LinkedList to simulate a print job queue. b) Add 5 print jobs using offer(). c) Process (remove) jobs one by one using poll(), printing each. d) Add 3 more jobs during processing. e) Use peek() to check the next job without removing. Exercise 3: List Operations ---------------------------- a) Create two lists: list1 = [1,2,3,4,5] and list2 = [3,4,5,6,7]. b) Find the union (combine both, no duplicates). c) Find the intersection (elements in both). d) Find the difference (elements in list1 but not list2). e) Use Collections.disjoint() to check if two lists share elements. ============================================================ PART B: Set Interface [30 minutes] ============================================================ Exercise 4: Unique Elements ---------------------------- a) Create a HashSet with 10 course names (include duplicates). b) Print the set - observe duplicates are removed. c) Check if a specific course exists using contains(). d) Create another HashSet and find: - Union: addAll() - Intersection: retainAll() - Difference: removeAll() Exercise 5: Sorted Set ----------------------- a) Create a TreeSet with random numbers. b) Print elements (observe they are sorted). c) Use first(), last(), headSet(50), tailSet(50), subSet(20, 80). d) Create a TreeSet with a custom Comparator to sort by GPA. Exercise 6: Custom equals/hashCode ------------------------------------ a) Create a Course class with code (String) and name (String). b) Add two Course objects with the same code to a HashSet. c) Observe that duplicates are NOT detected (both are added). d) Override equals() and hashCode() based on code. e) Test again - now duplicates are properly detected. ============================================================ PART C: Map Interface [30 minutes] ============================================================ Exercise 7: Word Frequency Counter ------------------------------------ Given text: "java is great java is powerful java collections are useful collections are great" a) Split the text into words. b) Use HashMap to count word frequencies. c) Print all entries sorted by frequency (descending). d) Find the most frequent word. e) Find words that appear exactly once. Exercise 8: Student Grades System ---------------------------------- a) Create HashMap> mapping student name to list of grades. b) Add 5 students with 4 grades each. c) Calculate and print average grade for each student. d) Find the student with highest average. e) Use getOrDefault() and putIfAbsent() to handle missing entries. Exercise 9: TreeMap for Sorted Data ------------------------------------- a) Create TreeMap mapping course names to averages. b) Insert 8 courses. c) Print in alphabetical order (automatic with TreeMap). d) Use firstKey(), lastKey(), subMap(), floorKey(), ceilingKey(). e) Iterate using entrySet() and print formatted table. ============================================================ PART D: Advanced Challenges [30 minutes] ============================================================ Exercise 10: Collection Conversion ------------------------------------ a) Convert ArrayList to HashSet (remove duplicates). b) Convert HashSet to ArrayList (for indexing). c) Convert Map keys to a List, sort it. d) Convert two arrays to a Map (one for keys, one for values). Exercise 11: Nested Collections --------------------------------- a) Create Map> mapping department to list of students. b) Add 3 departments with 4-5 students each. c) Find the department with the most students. d) Find all departments that contain a specific student. e) Print a formatted report of all departments and their students. Exercise 12: PriorityQueue Application ---------------------------------------- Create a task scheduling system: a) Define a Task class with name (String) and priority (int, lower = higher priority). b) Implement Comparable based on priority. c) Create PriorityQueue and add 8 tasks with different priorities. d) Process tasks in priority order using poll(). e) Add an urgent task (priority 0) mid-processing and observe queue reordering. ============================================================ SUBMISSION: Submit all .java files in a folder named Lab01_RegNo/ ============================================================