COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University

32
COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University [email protected] Office: Atanasoff 201

description

COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University [email protected] Office: Atanasoff 201. Motivation Example. This box allows one to store any kind of objects - PowerPoint PPT Presentation

Transcript of COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University

Page 1: 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

Page 2: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University

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?

Page 3: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University

Motivation Example

This ArrayList allows you to store any object, but this flexibility may result in run-time error

Page 4: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University

Generic TypesA generic type is a generic class or interface that is parameterized over types

Page 5: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University

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

Page 6: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University

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?

Page 7: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University

Naming Rules

Page 8: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University

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");

Page 9: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University

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

Page 10: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University

Exercises

Page 11: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University

Exercises

Page 12: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University

Exercises

Page 13: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University

Exercises

Page 14: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University

Exercises

Page 15: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University

How can we sort strings, e.g., “apple” < “bear”

Page 16: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University

Using Comparable Interface

Page 17: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University
Page 18: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University

Using Comparator Interface

Page 19: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University

Using Comparator Interface

Page 20: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University
Page 21: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University

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){}:::

Page 22: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University

Generic Sorting Methods

public static <T> void selectionSort ( T[] arr, Comparator<T> Comp )

Type Declaration

Page 23: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University

Implementation

Page 24: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University

For each value of T, supply its own Comparator

String arr[] = {“abc”, “1234”)selectionSort(arr, new LengthComparator())

Page 25: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University

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);

Page 26: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University

Truck[] arr;selectionSort(arr, myComp);

public static <T> void selectionSort ( T[] arr, Comparator<T> Comp )

Page 27: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University

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

Page 28: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University

Generic Sorting

Page 29: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University

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;

}}

Page 30: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University

public class Vehicle implements Comparable<Vehicle> { ... public int compareTo(Name n) { ... }}

Vehicle v[]; ...

selectionSort(v);

Using Comparable <T> interface

Page 31: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University

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?

Page 32: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University

Using Comparable <T> interface