Senior Project Design
Generic Programming

Course Map

Agenda

Type Variables

Generics is essentially the ability to have type parameters on your type.
They are also called parameterized types or parametric polymorphism.

Hiding Variability with Generic Programming

 

Instantiating Classes with Type Variables

Instantiating Type Variables in Methods

Type Variables Increase Safety

Syntax 22.1: Instantiating a Generic Class

GenericClassName<Type1, Type2, . . .>

Example:

 
ArrayList<BankAccount>
HashMap<String, Integer>

Purpose:

To supply specific types for the type variables of a generic class

Self Check

  1. The standard library provides a class HashMap<K, V> with key type K and value type V. Declare a hash map that maps strings to integers.
  2. The binary search tree  is an example of generic programming because you can use it with any classes that implement the Comparable interface. Does it achieve genericity through inheritance or type variables?

Answers

  1. HashMap<String, Integer>
  2. It uses inheritance.

Implementing Generic Classes

Good Type Variable Names

 Type Variable  Name Meaning
E     Element type in a collection    
K Key type in a map
V Value type in a map
T General type
S, U Additional general types

Class Pair

public class Pair<T, S>
{
public Pair(T firstElement, S secondElement)
{
first = firstElement;
second = secondElement;
}

public T getFirst() { return first; }
public S getSecond() { return second; }

private T first;
private S second;
}

Turning LinkedList into a Generic Class

public class LinkedList<E>
{
. . .
public E removeFirst()
{
if (first == null)
throw new NoSuchElementException();
E element = first.data;
first = first.next;
return element;
}
. . .
private Node first;

private class Node
{
public E data;
public Node next;
}
}

Implementing Generic Classes

Syntax: Defining a Generic Class

accessSpecifier class GenericClassName<TypeVariable1, TypeVariable2, . . .>
{
constructors
methods
fields

}

Example:

 
public class Pair<T, S>
{
. . .
}

Purpose:

To define a generic class with methods and fields that depend on type variables

File LinkedList.java

File ListIterator.java

File ListTester.java

Output

Dick
Harry
Juliet
Nina
Tom

Self Check

  1. How would you use the generic Pair class to construct a pair of strings "Hello" and "World"?
  2. What change was made to the ListIterator interface, and why was that change necessary?

Answers

  1. new Pair<String, String>("Hello", "World")
  2. ListIterator<E> is now a generic type. Its interface depends on the element type of the linked list.

Generic Methods

Generic Methods

Syntax: Defining a Generic Method

modifiers <TypeVariable1, TypeVariable2, . . .> returnType methodName(parameters)
{
body
}

Example:

 
public static <E> void print(E[] a)
{
. . .
}

Purpose:

To define a generic method that depends on type variables

Self Check

  1. Exactly what does the generic print method print when you pass an array of BankAccount objects containing two bank accounts with zero balances?
  2. Is the getFirst method of the Pair class a generic method?

Answers

  1. The output depends on the definition of the toString method in the BankAccount class.
  2. No–the method has no type parameters. It is an ordinary method in a generic class.

Constraining Type Variables

Constraining Type Variables

Self Check

  1. Declare a generic BinarySearchTree class with an appropriate type variable.
  2. Modify the min method to compute the minimum of an array of elements that implements the Measurable interface.

Answers

  1. public class BinarySearchTree<E extends Comparable>
  2. public static <E extends Measurable> E min(E[] a)
    {
    E smallest = a[0];
    for (int i = 1; i < a.length; i++)
    if (a[i].getMeasure() < smallest.getMeasure()) < 0)
    smallest = a[i];
    return smallest;
    }

Wildcard Types

Name Syntax Meaning
Wildcard with lower bound ? extends B Any subtype of B
Wildcard with upper bound ? super B Any supertype of B
Unbounded wildcard ? Any type

Wildcard Types

Raw Types

Raw Types

Self Check

  1. What is the erasure of the print method below?
    public static <E> void print(E[] a)
    {
    for (E e : a)
    System.out.print(e + " ");
    System.out.println();
    }
  2. What is the raw type of the LinkedList<E> class?

Answers

  1. public static void print(Object[] a)
    {
    for (Object e : a)
    System.out.print(e + " ");
    System.out.println();
    }
  2. The LinkedList class.