Generic Types in Java Higher-order Programming In Java The Stack ADT 04 A: Lists, Stacks, and Queues III CS1102S: Data Structures and Algorithms Martin Henz February 3, 2010 Generated on Monday 1 st February, 2010, 16:31 CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 1
Generic Types in Java Higher-order Programming In Java The Stack ADT 1 Generic Types in Java 2 Higher-order Programming In Java 3 The Stack ADT CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 2
Generic Types in Java Higher-order Programming In Java The Stack ADT 1 Generic Types in Java 2 Higher-order Programming In Java 3 The Stack ADT CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 3
Generic Types in Java Higher-order Programming In Java The Stack ADT A Simple Box Class public class IntegerBox { private Integer integer ; public void add ( Integer i ) { integer = i ; } public Integer get ( ) { return integer ; } } CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 4
Generic Types in Java Higher-order Programming In Java The Stack ADT Is this practical? Situation Each time we want to have a box for some data type, we need to define a MyTypeBox class. First Idea Use Object as the type of the elements. After all, any Java object is an Object! CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 5
Generic Types in Java Higher-order Programming In Java The Stack ADT An Object Box Class public class ObjectBox { private Object object ; public void add ( Object obj ) { object = obj ; } public Object get ( ) { return object ; } } CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 6
Generic Types in Java Higher-order Programming In Java The Stack ADT Using the Object Box public class ObjectBoxTest { public static void main ( String [ ] args ) { / / ONLY place Integer objects i n t o box ! ObjectBox integerBox = new ObjectBox ( ) ; integerBox . add ( new Integer ( 1 0 ) ) ; Integer someInteger = ( Integer ) integerBox . get ( ) ; System . out . p r i n t l n ( someInteger ) ; } } CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 7
Generic Types in Java Higher-order Programming In Java The Stack ADT Using the Object Box (2) / / ONLY place Integer objects i n t o t h i s box ! ObjectBox integerBox = new ObjectBox ( ) ; / / Imagine t h i s i s one part of large a p p l i c a t i o n / / modified by one programmer . integerBox . add ( ” 10 ” ) ; / / note type now String / / . . . and t h i s i s another , perhaps w r i t t e n / / by a d i f f e r e n t programmer Integer someInteger = ( Integer ) integerBox . get ( ) ; System . out . p r i n t l n ( someInteger ) ; CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 8
Generic Types in Java Higher-order Programming In Java The Stack ADT Is this practical? Situation In order to take items out of the box, we need to use a cast operation. This operation circumvents Java’s type system, and is not safe, especially in large programs! Idea Write a “generic” class that can be re-used for any kind of content type CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 9
Generic Types in Java Higher-order Programming In Java The Stack ADT A Generic Box public class Box < T > { private T t ; / / T stands f o r ” Type ” public void add (T t ) { this . t = t ; } public T get ( ) { return t ; } } CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 10
Generic Types in Java Higher-order Programming In Java The Stack ADT Using A Generic Box Box < Integer > integerBox = new Box < Integer > (); integerBox . add ( new Integer ( 1 0 ) ) ; Integer someInteger = integerBox . get ( ) ; / / no cast ! System . out . p r i n t l n ( someInteger ) ; / / t r y adding a String : / / integerBox . add ( ” some s t r i n g ” ) ; CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 11
Generic Types in Java Higher-order Programming In Java The Stack ADT How to write functions on boxes? Issue The Java operator new needs a proper class, not a generic one! Example public class BoxUtil { public static void f i l l B o x e s (U u , List < Box < U > boxes ) { > for (Box < U > box : boxes ) box . add ( u ) ; } } CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 12
Generic Types in Java Higher-order Programming In Java The Stack ADT How to write functions on boxes? Example public class BoxUtil { public static void f i l l B o x e s (U u , List < Box < U > boxes ) { > for (Box < U > box : boxes ) box . add ( u ) ; } } Problem Where does the type U come from? CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 13
Generic Types in Java Higher-order Programming In Java The Stack ADT Generic Method Example public class BoxUtil { public static < U > void f i l l B o x e s (U u , > boxes ) { List < Box < U > for (Box < U > box : boxes ) box . add ( u ) ; } } CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 14
Generic Types in Java Higher-order Programming In Java The Stack ADT Generic Method: Example class Crayon {} ; Crayon red = new Crayon ( ) ; List < Box < Crayon > > crayonBoxes = new ArrayList < Box < Crayon >> (); . . . BoxUtil . < Crayon > f i l l B o x e s ( red , crayonBoxes ) ; BoxUtil . f i l l B o x e s ( red , crayonBoxes ) ; / / compiler i n f e r s that U i s Crayon CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 15
Generic Types in Java Higher-order Programming In Java The Stack ADT An Issue: Subtyping public class Subtyping { public static void someMethod (Number n) { System . out . p r i n t l n ( n ) ; } public static void main ( String [ ] args ) { Object someObject = new Object ( ) ; someInteger = new Integer ( 1 0 ) ; Integer someObject = someInteger ; / / OK someMethod ( new Integer ( 1 0 ) ) ; / / OK someMethod ( new Double ( 1 0 . 1 ) ) ; / / OK } } CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 16
Generic Types in Java Higher-order Programming In Java The Stack ADT An Issue: Subtyping public class Subtyping { public static void boxTest (Box < Number > n) { System . out . p r i n t l n ( n ) ; } public static void main ( String [ ] args ) { Box < Number > box = new Box < Number > (); box . add ( new Integer ( 1 0 ) ) ; / / OK box . add ( new Double ( 1 0 . 1 ) ) ; / / OK boxTest ( new Box < Integer > ( ) ) ; / / NOT OK } } CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 17
Generic Types in Java Higher-order Programming In Java The Stack ADT Subtyping Explanation: Cages and Animals public class Animal {} public class Lion extends Animal {} public class extends Animal {} B u t t e r f l y public class Cage < E > extends HashSet < E > implements Collection < E > {} CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 18
Generic Types in Java Higher-order Programming In Java The Stack ADT Subtyping Explanation: Cages and Animals king = new Lion ( ) ; Lion Animal a = king ; Cage < Lion > lionCage = new Cage < Lion > (); lionCage . add ( king ) ; monarch = new B u t t e r f l y ( ) ; B u t t e r f l y Cage < B u t t e r f l y > butterflyCage = new Cage < B u t t e r f l y > (); butterflyCage . add ( monarch ) ; CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 19
Generic Types in Java Higher-order Programming In Java The Stack ADT Subtyping Explanation: Cages and Animals Cage < Animal > animalCage = new Cage < Animal > (); animalCage . add ( king ) ; animalCage . add ( monarch ) ; compile − time animalCage = lionCage ; / / error animalCage = butterflyCage ; / / compile − time error CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 20
Generic Types in Java Higher-order Programming In Java The Stack ADT 1 Generic Types in Java 2 Higher-order Programming In Java 3 The Stack ADT CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 21
Generic Types in Java Higher-order Programming In Java The Stack ADT Squaring the Elements of a List List < Integer > myIntegerList = new ArrayList < Integer > (); myIntegerList . add ( 4 ) ; myIntegerList . add ( 1 3 ) ; myIntegerList . add ( 5 ) ; List < Integer > myIntegerListSquare = ListMap . integerListSquare ( myIntegerList ) ; CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 22
Generic Types in Java Higher-order Programming In Java The Stack ADT Squaring the Elements of a List public class ListMap { public static List < Integer > integerListSquare ( List < Integer > a ) { ArrayList < Integer > r e s u l t L i s t = new ArrayList < Integer > (); for ( Integer i : a ) r e s u l t L i s t . add ( i ∗ i ) ; return r e s u l t L i s t ; } } CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 23
Recommend
More recommend