Download - COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University
COM S 228Generics
Instructor: Ying Cai
Department of Computer ScienceIowa State [email protected]: Atanasoff 201
Motivation Example
This box allows one to store any kind of objectsWhat happens if we want a kind of Box that can store only some specific kind of object?
Motivation Example
This ArrayList allows you to store any object, but this flexibility may result in run-time error
Generic TypesA generic type is a generic class or interface that is parameterized over types
Box<Integer> integerBox; // box of integerintegerBox = new Box<Integer>(); // instantiate a box of integerInteger i = new Integer(1);IntegerBox.set(i);
Box<String> stringBox = new Box<String>(); // box of stringstringBox = new Box<String>();
stringBox.set(“here is a string”);stringBox.set(i); // compile-time error
ArrayList arr = new ArrayList(): // any object can be stored ArrayList<T>: // only the object of type T can be store
ArrayList<String> s = new ArrayList<String>();s.add(“here”);s.add(“there”);s.add(new Integer(1)); // could be an error???s.add(1); // could be an error???
What flexibility is removed?
Naming Rules
Multiple Type Parameters
Pair<String, Integer> p1 = new OrderedPair<String, Integer>("Even", 8);Pair<String, String> p2 = new OrderedPair<String, String>("hello", "world");orOrderedPair<String, Integer> p1 = new OrderedPair<>("Even", 8); OrderedPair<String, String> p2 = new OrderedPair<>("hello", "world");
Raw TypeA raw type is the name of a generic class or interface without any type arguments.
Box<Integer> integerBox = new Box<>(); // a parameterized boxBox rawBox = new Box(); // a raw box
Box<String> stringBox = new Box<>();rawBox = stringBox; // OK
integerBox = rawBox; // rawBox is a raw type of Box<T>; // warning: uncheck conversion
Exercises
Exercises
Exercises
Exercises
Exercises
How can we sort strings, e.g., “apple” < “bear”
Using Comparable Interface
Using Comparator Interface
Using Comparator Interface
Sorting Implementations// Solution 1: For each class, implements its own Sortingint[] selectionSort(int[] arr) {}String[] selectionSort(String[]) {}String[] selectionSort(String[], Comparator) {}Vehicle[] selectionSort(Vehicle[], PriceComparator){}Vehicle[] selectionSort(Vehicle[], WeightComparator){}:::
Generic Sorting Methods
public static <T> void selectionSort ( T[] arr, Comparator<T> Comp )
Type Declaration
Implementation
For each value of T, supply its own Comparator
String arr[] = {“abc”, “1234”)selectionSort(arr, new LengthComparator())
Problems
// suppose you have implemented a Vehicle class // and its comparatorpublic class Vehicle { ... };public VehicleComparator implements Comparator<String> { ... };
// you have also implemented a Truck classpublic class Truck extends Vehicle { ... }
// now you want to sort an array of trucksTruck[] arr;myComp = new VehicleComparator();selectionSort(arr, myComp);
Truck[] arr;selectionSort(arr, myComp);
public static <T> void selectionSort ( T[] arr, Comparator<T> Comp )
public static <T> void selectionSort ( T[] arr, Comparator<? super T> Comp )
Wild Cards and Bounds
Rule of Thumb: If T is a type parameter and you write Comparator<T>, you may want to use Comparator<? super T>.
Generic Sorting
Using Comparable <T> interface
public static void selectionSort(T[] arr) { for (int i=0; i<arr.length-1; i++) {
int minIndex = i;for (int j=i+1; j<arr.length; j++) { if (arr[j].compareTo(arr[minIndex]) <
0) {minIndex = j;
}}
T temp = arr[i];arr[i] = arr[minIndex];arr[minIndex] = temp;
}}
public class Vehicle implements Comparable<Vehicle> { ... public int compareTo(Name n) { ... }}
Vehicle v[]; ...
selectionSort(v);
Using Comparable <T> interface
Using Comparable () interface
public class Vehicle implements Comparable<Vehicle> { ... public int compareTo(Name n) { ... }};public class Truck extends Vehicle { ... }
// now you want to sort an array of TrucksTruck t[]; ...
selectionSort(t); // does this work?
Using Comparable <T> interface