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.

Practise Exercises
  • 1) Write tests for AIntegerList found inside the package lists.genericinterface.intlist

  • 2) Write tests for List found inside the package lists.finalgenericlist. Make sure you write tests for multiple different kinds of lists including lists of lists of list …​ etc.

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.

Practise Exercises
  • 3) Alter List and its implementation so that user can use List with any Java reference type.

  • 4) Provide black box tests for your new generic implementation of List.

  • 5) Add a new operation to your generic List called removeAll(). The new operation should consume one argument which is another list with elements of the same type as our list. The operation should update our current list and remove all elements that are also found in the argument list, e.g., (1 1 2 3 4 5).removeAll1, 4 should return (2 3 5).

2. Java Util

2.1. Lists

Create a class called ListUtils to place all methods that you need to design for the following exercises.

Practise Exercises
  • 6) Using List from java.util write a method that consumes two List s, l1 and l2 and returns a new list that contains all the elements in l1 that are not in l2 and all the elements in l2 that are not in l1

  • 7) Using List from java.util write a method that removes all duplicates element from the list.

  • 8) Using List from java.util write a method that consumes a List of elements and a number n and returns a new list with each element repeated n times, e.g, given (1 2 3) and 4 the method should return (1 1 1 1 2 2 2 2 3 3 3 3).

  • 9) Using List from java.util write a method that given a List<List<X>> flattens the list, e.g., given ( (1 2 3) (3 4 5)) will return (1 2 3 4 5).

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

  1. Creates a Map<String, Integer>.

  2. Adds 3 key-value pairs.

  3. Iterates over its pairs and prints them out to the Console.

Using 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.

Practise Exercises
  • 10) Using Map from java.util write a method that given a list of String will generate a table of each word in the list and the frequency (number of times) of that word in the list. For example given ("a", "b", "c", "a", "b") the method should output

    "a" 2
    "b" 2
    "c" 1

    The order that you print out the words does not matter.

  • 11) Using Map from java.util write a method that given a Map<String,Integer> that represents the index of a book, returns back a new Map<Integer,List<String>> that contains the page number and all the words at that page number as a list.