The goal of these exercises is to familiarise ourselves with:
- the Java Collections Framework
- and learn to distinguish working with interfaces and actual implementations.
We've provided the starter project above. It contains:
-
the CollectionsExercises class with four methods to implement:
useLinkedList
,useStack
,useArrayDeque
anduseHashMap
-
and a test suite to verify your implementation CollectionsExercisesTest.
In each method, replace the throw new RuntimeException("Not implemented")
with your code.
Hint
When printing on the screen, use the method that does not introduce line breaks.
In our earlier examples on lists, we gave our list a List
(of Integer
) type:
List<Integer> integers = new LinkedList<>();
We can also declare our list as a LinkedList
- which means that:
- our
integers
list is of typeList
, - and it is a
LinkedList
specifically.
LinkedList<Integer> integers = new LinkedList<>();
This allows us to access functionality that is specific to LinkedList
, and may not be available on the List
type.
Using the LinkedList Java documentation as a guide, implement the useLinkedList
method as described below.
public LinkedList<Integer> useLinkedList() {
// TODO: create an empty linked list
// - add 4 as the first element of the list
// - then add 5, 6, 8, 2, 9 to the the list
// - add another 2 as the last element of the list
// - add 4 as the 3rd element of the list
// - invoke the method element() on the list and print the result on the screen
// - return the list
throw new RuntimeException("Not implemented");
}
Using the Stack Java documentation as a guide, implement the useStack
method as described below.
public Stack<Integer> useStack() {
// TODO: create an empty stack
// - add 5, 6, 8, 9 to the the stack
// - print the first element of the stack on the screen
// - print the last element of the stack on the screen
// - invoke the method pop() on the stack and print the result on the screen
// - invoke the push(4) method on the stack
// - return the stack
throw new RuntimeException("Not implemented");
}
Using the ArrayDeque Java documentation as a guide, implement the useArrayDeque
method as described below.
public ArrayDeque<Integer> useArrayDeque() {
// TODO: create an empty arrayDeque
// - add 5, 6, 8, 9 to the the stack
// - print the first element of the queue on the screen
// - print the last element of the queue on the screen
// - invoke the method poll() on the queue and print the result on the screen
// - invoke the element() method on the queue and print the result on the screen
// - return the queue
throw new RuntimeException("Not implemented");
}
Using the HashMap Java documentation as a guide, implement the useHashMap
method as described below.
public HashMap<Integer, String> useHashMap() {
// TODO: create an empty hash map
// - add {1, TypeScript} entry to the map
// - add {2, Kotlin} entry to the map
// - add {3, Python} entry to the map
// - add {4, Java} entry to the map
// - add {5, JavaScript} entry to the map
// - add {6, Rust} entry to the map
// - determine the set of keys from the map and print it on the screen
// - determine the set of keys from the map and print it on the screen
// - determine whether the map contains "English" as a language and print the result on the screen
// - return the map
throw new RuntimeException("Not implemented");
}
To verify that your code works as expected, run the CollectionsExercisesTest
tests.
In your terminal, ensure that you are in the java-collections
folder.
Then run the following command in your terminal.
./mvnw clean test
If you are on Windows, run this command instead:
mvnw.cmd clean test
Your implementation is correct when all tests pass:
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] ├─ Collections Exercises - 0.027s
[INFO] │ ├─ ✔ working with HashMaps - 0.013s
[INFO] │ ├─ ✔ returns this unit's name - 0.001s
[INFO] │ ├─ ✔ working with ArrayDeques - 0.001s
[INFO] │ ├─ ✔ working with Stacks - 0s
[INFO] │ └─ ✔ working with LinkedLists - 0s
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 5, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.274 s
[INFO] Finished at: 2023-04-29T22:05:04+01:00
[INFO] ------------------------------------------------------------------------