Eric Roberts Handout #33 CS 106A February 5, 2016 Abstract Data Types
Outline
1. Type abstraction Abstract Data Types 2. Lists, sets, and maps (Lists, Sets, and Maps) 3. Parameterized types, wrapper classes, and autoboxing 4. Creating collections 5. Iteration 6. Applications and exercises Eric Roberts 7. Story time: Past Graphics Contest winners CS 106A February 5, 2016 Schedule Change To give everyone a little more time to create the greatest graphics contest entries of all time, I’m moving the due date for the Graphics Contest to Wednesday, February 10.
Type Abstraction Examples of Lists, Sets, and Maps • One of the most important advantages of the object-oriented • The conceptual list, set, and map types come up all the time paradigm is the idea of type abstraction, in which the goal is when you are trying to model aspects of the real world. to think about types in terms of their high-level behavior rather than their low-level implementation. • Consider, for example, the Enigma machine I talked about on Wednesday. I argue that each of these conceptual types occur: • In computer science, types that are defined by their behavior – The rotors inserted into the machine form a list. The order of are called abstract data types or ADTs. Our goal in this class the rotors matters, and it makes sense to talk about a first, will be to use abstract data types as much as possible, leaving second, and third rotors. the issues of how those types are implemented to CS 106B. – The stock rotors available to the operators form a set. Although • In this class, my goal is to introduce three abstract data types: the stock rotors are numbered for convenience, they can be used in any order and there is no duplication. – The List ADT is used to represent collections of values in which the order is typically important. – The permutation implemented by the rotors is a map. Each rotor takes an input letter that serves as the key and associates it – The Set ADT is used to represent unordered collections of with some other letter that represents the value. distinct values. – The Map ADT is used to represent collections in which the values are associated with identifying keys.
Exercise: Real-World Examples Parameterized Types
• What real-world examples can you identify for each of the • The List, Set, and Map ADTs represent collections of values. abstract types List, Set, and Map? As with all Java values, the elements contained in a collection • Talk with your neighbors for the next few minutes and come have data types, and it is necessary to specify those types up with at least one example of each type. whenever you use a collection type. Java calls such types parameterized types. • In Java, the complete name of a parameterized type includes the data type of the value written in angle brackets after the type name. For example, a list containing strings would have the type List
Wrapper Classes Autoboxing • In Java, parameterized types can be used only with object • The existence of wrapper classes makes it possible to define types and not with primitive types. Thus, while it is perfectly collections of the primitive types. You can, for example, legal to use the type name declare a list of integers using the type name List