1
CSC311: DATA STRUCTURES
LAB04_Getting Started with Java Collections Framework
Issue Date: 7 December 2022
Due Date: refer to Moodle LMS
Introduction
Generics or templates provide a mechanism for defining data structures as containers for values of any
type. In this regard, we need only one definition instead of multiple definitions. The example that
follows illustrates the basic syntax of template list data structure definition in C++.
Normal Structure definition in C++ Generic Template Structure definition in C++
// Multiple definitions-one for each data // One generic definition- works for
type. all data types.
const int MaxSize = 15;
const int MaxSize = 15;
//list of integer values
struct List { template <typename T>
int Size; struct List
int Item [MaxSize]; {
}; int Size;
T Item [MaxSize];
//list of string values };
struct List {
int Size;
String Item [MaxSize];
};
//list of student records
In the previous course, CSC213, we discussed how to define and use templates in C++. We also
explored and used the C++ standard template library (STL). In this lab session we want to learn more
about the Java Collections Framework. This framework is a library of generic/template data
structures. It is equivalent to the STL in C++.
2
TASK
Your task this week is to explore, and learn as much as possible about the Java Collections List
interface and its implementation classes: LinkedList, ArrayList and Vector as shown in figure
below.
In this regard, create a simple Java application project and use the Java documentation to help you do
the following: [Note: provide comments to explain what you are doing in each code segment]
1. Declare a list of integers, say L, and initialize it to be a LinkedList. That is, use the LinkedList
implementation class constructor. How many constructors does the LinkedList class have? If
more than 1, write code to experiment initializing L using each of them.
2. Write simple code to add new values to list L. How many different methods can be used to
add values to a List? If more than 1, add elements to list L using each of them. In any case,
make sure list L has at least 5 values.
3. Write simple code to loop through, and display, all values in the list using the following
constructs: refer to this URL for examples: https://www.baeldung.com/java-iterate-list
(i) toString() method:- L.toString()
(ii) basic for loop:- for ( ; ;)
(iii) enhanced for loop :- for (int x: L)
(iv) while loop:- while ( )
(v) Iterator:- Iterator<Integer> it = L.iterator();
(vi) ListIterator:- ListIterator< Integer > lit = L.listIterator();
(vii) Iterable.forEach method : L.foreach(System.out::println)
(viii) Stream.forEach : L.stream().forEach((c) -> System.out.println(c));
3
4. What other methods can be used with a List object. Write simple code to demonstrate and
output results? Remember to provide comments.—demonstrate 3 or 4 other methods not
already used.
5. What List algorithms are provided in java.util.Collections class? Write simple code to
demonstrate and output results. Look at algorithms like Collections.sort(L),
Collections.shuffle(L), etc. Demonstrate the use of 3 or 4 other algorithms not already
mentioned or used. After applying each algorithm, display the results.
6. Change the initialization in step 1 from LinkedList to ArrayList. Does your script still run
without any further changes? If not, explain why?
7. Change the initialization in step 1 from LinkedList to Vector. Does your script still run
without any further changes? If not, explain why? Using the Vector class is not
recommended, explain why?
8. In your project, add a new class called Student whose data members/variables include student
identity number, first name, surname and age. Thereafter, write code for the following
(i) Add toString() method to class Student
(ii) add constructors for class Student
(iii) add getters and setters for class student
9. In the main function for your project do the following [remember to add comments]
(i) Declare a student list, say studList, and initialize it to be a linked list of students.
(ii) Initialize and add at least 5 student records to studList
(iii) Write a loop that uses an iterator to display all records in studList
(iv) Use the sort algorithm to sort studList. Hint :- it may need a comparator. And may
also require that you override equals() and hashcode() methods to class Student.
(v) What other algorithms can you apply to studList?
10. In step 9(i), does your code work if you change from LinkedList to ArrayList? Explain why it
still works or does not work.
SUBMISSION INSTRUCTIONS
Refer to instructions in Moodle LMS.