Generics

From Java Programming II
Jump to: navigation, search

Generic Classes and Methods in Java

Learning Outcomes

At the end of this chapter, the student will be able to:

  • To create generic methods that perform identical tasks on arguments of different types.
  • To create a generic Stack class that can be used to store objects of any class or interface type.
  • To understand how to overload generic methods with nongeneric methods or with other generic methods.
  • To understand raw types and how they help achieve backward compatibility.
  • To use wildcards when precise type information about a parameter is not required in the method body.

Lecture notes

Exercises

Linear Search Generic Method

Implement the following generic method for linear search. public static <E extends Comparable<E>> int linearSearch(E[] list, E key)

Stack Class

Write a generic class called Stack<T> that represents a stack structure. A stack structure follow the strategy last-in-first-out, which means that the last element added to the stack, if the first to be taken out. The stack class has the following attributes and methods:

  • An attribute ArrayList<T> elements which represents the elements of the of the stack.
  • A constructor that creates the ArrayList
  • a method push(T e) which adds the element to the ArrayList<T>
  • a method pop() which removes the last element of the ArrayList<T> (last element added), if the list is not already empty and returns it.
  • a method print() which prints the elements of the stack starting from the last element to the first element.


Create a class StackDemo, in which you create two stacks, one stack of String and one Stack of students, add elements, print, then remove all elements and then print.

Stackable Interface

In the previous exercise, you create the Stack<T> generic class. In this exercise, you will create the generic interface Stackable<T> that contains the abstract methods:

  • an abstract method push(T e) which adds the element to the ArrayList<T>
  • an abstract method pop() which remove the last element of the ArrayList<T> (last element added), if the list is not already empty.
  • a abstract method print() which prints the elements of the stack starting from the last element to the first element.
  • a abstract method isEmpty() that would return true if the stack is empty, and false otherwise.

Write the interface Stackable<T>.

Modify the class Stack<T> such that it implements the generic interface Stackable<T>.


Map

Interface
In this exercise, you will create a generic Map interface Map<K,V> that represents a Map structure. K is the type for a key, V is the type of a value.
A key is unique in a map and cannot be repeated.
The map contains the following abstract methods.

  • an abstract method add(K key, V value) which adds the element to the map
  • an abstract method remove(K key) which removes the element with the specified key from the map and returns the value removed.
  • a abstract method size() which returns the size of the map
  • a abstract method isEmpty() that would return true if the map is empty, and false otherwise.
  • a abstract method keys() that returns the list of all keys.
  • a abstract method print() that prints all the elements of the map.



Class Implementation
Now, write the generic class MapImpl that implements the interface Map. Use an ArrayList to store the keys, and another ArrayList to store the values.


Test Class
Write a test class that creates two Maps.

  • Map1: <String, Integer> where the key is a String and the value is an Integer
  • Map2: <Integer, Double> where the key is a String and the value is an Integer

Add several elements. Try to add elements with the same key, and check that only one instance is added effectively with no redundancy.

Videos