JP-II Lab5
JP-II Lab5
JAVA PROGRAMMING II
Module 5: java.util and Collections API (Part1)
Lab Guide for Lab5
Session Objectives
In this session, you will be practicing with Java Collection API for managing group of
objects, includes:
⮚ Introduction to java.util Packages
⮚ Set
⮚ Map
The following program uses ArrayList to store the String objects, so we can use some
useful methods such as add() contains() or indexOf(). We also use Collections.sort() to
sort a List.
Scan the code first, copy/paste the code, compile, run , observe the result and discuss with your
classmate or instructor.
File: LyricWord.java
import java.util.*;
© FPT-Aptech Page 1 / 5
JP-II-Lab5 – java.util and Collections API (Part 1)
//Sort words
Collections.sort(words);
The following program uses HashSet to store the String objects, so we can use some
useful methods such as add() contains() to add an element to set and check if it is in.
Scan the code first, copy/paste the code, compile, run , observe the result and discuss with your
classmate or instructor.
File: LyricSet.java
import java.util.*;
© FPT-Aptech Page 2 / 5
JP-II-Lab5 – java.util and Collections API (Part 1)
The following program uses HashMap to store the information about the planets, each
planet is described by a unique name so we can access it easily.
Scan the code first, copy/paste the code, compile, run , observe the result and discuss with your
classmate or instructor.
File: PlanetDiameters.java
import java.util.*;
© FPT-Aptech Page 3 / 5
JP-II-Lab5 – java.util and Collections API (Part 1)
if (!planets.containsKey(name))
System.out.println("Planet " + name + " not found!");
else
System.out.println("The diameter of " + name +
" is " + planets.get(name));
}
}
}
● Quickly look at workshops of Module 5 for reviewing basic steps for creating and using
collections in specific cases.
● Try to compile, run and observe the output of sample code provided for related
workshops. Discuss with your class-mate and your instructor if needed.
Do the assignment for Module 5 carefully. Discuss with your class-mates and your instructor
if needed.
© FPT-Aptech Page 4 / 5
JP-II-Lab5 – java.util and Collections API (Part 1)
Part 4 – Do it yourself
Exercise 1:
Using the Collections Framework, write a Student Management program that has the
following functional menu:
1. Add Students
2. Update a Student
3. Delete a Student
4. Search Students
5. Display All Students
6. Save to File
7. Load from File
8. Exit
Your choice: _
For each student, the program should maintain RollNumber, Name, Age, and Mark.
Try to use some collection types such as List, Map, Set and compare these solutions.
Then find the best way to solve this problem.
© FPT-Aptech Page 5 / 5