1. Using generics
1.1. Cons Lists
We developed mutliple versions of our List implementations using Generics. The code from our lectures is available in the lecture notes repo.
Using the code in the lecture notes repo under folder 07
attempt the following exercises.
1.2. Lists with mutation
In lecture 6 we developed another implemenation for lists of integers that uses mutation. The code for lecture 6 is available in the lecture notes repo.
Using the code in the lecture notes repo under folder 06
attempt the following exercises.
2. Java Util
2.1. Lists
Create a class called ListUtils
to place all methods that you need to design for the following exercises.
2.2. Maps
There are mutliple ways to iterate over the key-value pairs inside Java’s Map
, a very concise way to iterate over the elements of a Map
is to use
Map.Entry
and the method entrySet()
defined on Map
.
Here is small example that
-
Creates a
Map<String, Integer>
. -
Adds 3 key-value pairs.
-
Iterates over its pairs and prints them out to the Console.
Map<String,Integer>
.public class Test {
public static void main(String[] args) {
// Create a map. We assign to a variable
// with type `Map`
Map<String, Integer> index = new HashMap<>();
index.put("a", 2); // add "a" mapped to the integer 2
index.put("b", 3); // add "b" mapped to the integer 3
index.put("c", 4); // add "c" mapped to the integer 4
for (Map.Entry<String, Integer> entry: index.entrySet()) // iterate over
System.out.println(entry.getKey() + "," + entry.getValue()); // print key and value
}
}
The above code outputs
a,2 b,3 c,4
Create a class called MapUtils
to place all methods that you need to design for the following exercises.