Data Structures and Abstractions with Java Java Interlude
Data Structures and Abstractions with Java™ Java Interlude 5 More About Generics 5 th Edition Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
The Interface Comparable • Consider the method compare. To for class String • if s and t are strings, s. compare. To(t) is – Negative if s comes before t – Zero if s and t are equal – Positive if s comes after t Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
The Interface Comparable • By invoking compare. To, you compare two objects of the class T. • LISTING JI 5 -3 The interface java. lang. Comparable public interface Comparable<T> { public int compare. To(T other); } // end Comparable Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
The Interface Comparable • Create a class Circle, define compare. To public class Circle implements Comparable<Circle>, Measurable { private double radius; // Definitions of constructors and methods are here. //. . . public int compare. To(Circle other) { int result; if (this. equals(other)) result = 0; else if (radius < other. radius) result = -1; else result = 1; return result; } // compare. To } // end Circle Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Generic Methods public class Example { public static <T> void display. Array(T[] an. Array) { for (T array. Entry : an. Array) { System. out. print(array. Entry); System. out. print(' '); } // end for System. out. println(); } // end display. Array public static void main(String args[]) { String[] string. Array = {"apple", "banana", "carrot", "dandelion"}; System. out. print("string. Array contains "); display. Array(string. Array); Character[] character. Array = {'a', 'b', 'c', 'd'}; System. out. print("character. Array contains "); display. Array(character. Array); } // end main } // end Example Listing JI 5 -2 An example of a generic method Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Bounded Type Parameters Consider this simple class of squares: public class Square<T> { private T side; public Square(T initial. Side) { side = initial. Side; } // end constructor public T get. Side() { return side; } // end get. Side } // end Square Different types of square objects possible. Square<Integer> int. Square = new Square<>(5); Square<Double> real. Square = new Square<>(2. 1); Square<String> string. Square= new Square<>("25"); Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Bounded Type Parameters Imagine that we want to write a static method that returns the smallest object in an array. Suppose that we wrote our method shown here: public My. Class { // First draft and INCORRECT: public static <T> T array. Minimum(T[] an. Array) { T minimum = an. Array[0]; for (T array. Entry : an. Array) { if (array. Entry. compare. To(minimum) < 0) minimum = array. Entry; } // end for return minimum; } // end array. Minimum } // end My. Class Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Bounded Type Parameters Header really should be as shown public My. Class { public static <T extends Comparable<T>> T array. Minimum(T[] an. Array) { T minimum = an. Array[0]; for (T array. Entry : an. Array) { if (array. Entry. compare. To(minimum) < 0) minimum = array. Entry; } // end for return minimum; } // end array. Minimum //. . . } // end My. Class Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Wildcards • Question mark, ? , is used to represent an unknown class type – Referred to as a wildcard • Consider following method and objects public static void display. Pair(Ordered. Pair<? > pair) { System. out. println(pair); } // end display. Pair Ordered. Pair<String> a. Pair = new Ordered. Pair<>("apple", "banana"); Ordered. Pair<Integer> another. Pair = new Ordered. Pair<>(1, 2); • Method display. Pair will accept as an argument a pair of objects whose data type is any one class display. Pair(a. Pair); display. Pair(another. Pair); Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Bounded Wildcards <<interface>> Comparable<T> +compare. To(other: T): integer Widget +compare. To(other: Widget): integer Gadget FIGURE J 5 -1 The class Gadget is derived from the class Widget, which implements the interface Comparable Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
End Java Interlude 5 Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
- Slides: 11