COMP 110401 COLLECTION KINDS Instructor Prasun Dewan PREREQUISITE

  • Slides: 41
Download presentation
COMP 110/401 COLLECTION KINDS Instructor: Prasun Dewan

COMP 110/401 COLLECTION KINDS Instructor: Prasun Dewan

PREREQUISITE Arrays Collections Implementation 2

PREREQUISITE Arrays Collections Implementation 2

COLLECTION TYPES String. History, String. Database, String. Set Array. List, List Map Stack Queue

COLLECTION TYPES String. History, String. Database, String. Set Array. List, List Map Stack Queue 3

STRUCTURED VS. ATOMIC TYPES Atomic Types Structured Types Primitive types double Classes int ACartesian.

STRUCTURED VS. ATOMIC TYPES Atomic Types Structured Types Primitive types double Classes int ACartesian. Point String. History Classes ABMICalculator Interfaces BMICalculator Point AString. History Instances of structure type decomposed into one or more smaller values 4

LOGICAL STRUCTURE POINT int Property name X Y Point Angle double Radius double Interface,

LOGICAL STRUCTURE POINT int Property name X Y Point Angle double Radius double Interface, Class or primitive type of property value 5

LOGICAL STRUCTURE ARRAY int index 0 1 {5, 6, 7, 8} 2 int 3

LOGICAL STRUCTURE ARRAY int index 0 1 {5, 6, 7, 8} 2 int 3 int Interface, Class or primitive type of property value 6

BEAN VS. ARRAY int int X 0 Y Point 1 {5, 6, 7, 8}

BEAN VS. ARRAY int int X 0 Y Point 1 {5, 6, 7, 8} Angle double Radius 2 int 3 double Property Name int Heterogenous Components can be of different types and serve different functions Homogenous Index Components are of same type (which may be a super type of the specific types of the components) and are handles in the same way Object[] objects = { “Joe Doe”, new AString. Database(), new AString. History()}; 7

BEAN VS. (INDEXED) COLLECTION point. get. X() point. set. Y(100) Bean: Program fragment can

BEAN VS. (INDEXED) COLLECTION point. get. X() point. set. Y(100) Bean: Program fragment can refer to only a particular kind of component of composite object scores[position + 1] Collection: Program fragment can refer to any component of the composite object Indexed collection: components referred explicitly by int expressions 8

INDEXED COLLECTIONS Each element identified by a unique index Successive elements have consecutive indices

INDEXED COLLECTIONS Each element identified by a unique index Successive elements have consecutive indices 9

STRINGHISTORY? size() element. At() String History add. Element() 10

STRINGHISTORY? size() element. At() String History add. Element() 10

STRINGHISTORY String index 0 1 string. History 2 3 String string. History. element. At(position

STRINGHISTORY String index 0 1 string. History 2 3 String string. History. element. At(position + 1) Collection: Program fragment can refer to any component of the composite object Indexed collection: components referred explicitly by int expressions 11

STRINGHISTORY VS. ARRAY size() element. At() String History add. Element() Dynamic: After creation, can

STRINGHISTORY VS. ARRAY size() element. At() String History add. Element() Dynamic: After creation, can grow Programmer-defined String[] strings= { “James Dean”, “Joe Doe”, “Jane Smith”)}; Static and Language-defined 12

STATIC VS. DYNAMIC STRUCTURES Static Beans have fixed number of properties Arrays have fixed

STATIC VS. DYNAMIC STRUCTURES Static Beans have fixed number of properties Arrays have fixed number of elements Though an array variable can be assigned arrays of different sizes Dynamic Can create new edges in logical structure 13

HISTORY public interface String. History { public void add. Element(String element); public int size();

HISTORY public interface String. History { public void add. Element(String element); public int size(); public String element. At(int index); } 14

DATABASE public interface String. Database { //from history public int size(); public void add.

DATABASE public interface String. Database { //from history public int size(); public void add. Element(String element); public String element. At(int index); //additional methods public void remove. Element(String element); public boolean member(String element); public void clear(); } Do we need a history if we have a database? Yes, principle of least privilege 15

PRINCIPLE OF LEAST PRIVILEGE/ NEED TO KNOW Do not give a user of some

PRINCIPLE OF LEAST PRIVILEGE/ NEED TO KNOW Do not give a user of some code more rights than it needs � Code is easier to change � Need to learn less to use code � Less likelihood of accidental or malicious damage to program Like hiding engine details from car driver 16

VISIBILITY OF A TYPE AND ITS MEMBERS: SOME TYPE ARE MORE EQUAL THAN OTHERS

VISIBILITY OF A TYPE AND ITS MEMBERS: SOME TYPE ARE MORE EQUAL THAN OTHERS private default All classes protected Increasing access Subtypes Co-Packaged public Co-packaged a la co-workers, amplifier + speakers Subtype a la family, deluxe amplifier Encapsulation Rule: Do not make variables of a class public 17

USING DATABASE AS HISTORY public interface String. Database { //from history public int size();

USING DATABASE AS HISTORY public interface String. Database { //from history public int size(); public void add. Element(String element); public String element. At(int index); //additional methods public void remove. Element(String element); public boolean member(String element); public void clear(); } Programmer would be able to perform inappropriate operations on a logical history implemented physically as a database 18

CO-EXISTENCE public interface String. Database extends String. History { //additional methods public void remove.

CO-EXISTENCE public interface String. Database extends String. History { //additional methods public void remove. Element(String element); public boolean member(String element); public void clear(); } Programmer would be able to perform inappropriate operations on a logical history implemented physically as a database 19

VECTOR: GENERAL OBJECT COLLECTION public final int size(); public final Object element. At(int index);

VECTOR: GENERAL OBJECT COLLECTION public final int size(); public final Object element. At(int index); public final void add. Element(Object obj) ; public final void set. Element. At(Object obj, int index); public final void insert. Element. At(Object obj, int index); public final boolean remove. Element(Object obj); public final void remove. Element. At(int index); public final int index. Of(Object obj); … Do we need other collections if we have Vector Yes, principle of least privilege Yes, implementation considerations 20

CLASS ARRAYLIST AND VECTOR (LIST) public final int size(); public final Object get(int index);

CLASS ARRAYLIST AND VECTOR (LIST) public final int size(); public final Object get(int index); public final void add(Object obj) ; public final void set(int index, Object obj); public final void insert(int index, Object obj); public final boolean remove(Object obj); public final void remove(int index); public final int index. Of(Object obj); … Vector has Array. List (List) methods plus the additional original methods in the previous slides Can add arbitrary objects to these collections 21

ARRAY LIST AND VECTOR USER import java. util. Array. List; import java. util. Vector;

ARRAY LIST AND VECTOR USER import java. util. Array. List; import java. util. Vector; public class Vector. Array. List. User { public static void main (String[] args) { List names = new Vector(); List grand. Slams = new Array. List(); names. add("Nadal"); grand. Slams. add(13); names. add("Federer"); grand. Slams. add(17); names. add(“Borg"); grand. Slams. add(11); names. add(“Sampras"); grand. Slams. add(14); } } What kind of dynamic structure is being simulated? 22

INDEXED COLLECTIONS Each element identified by a unique index Successive elements have consecutive indices

INDEXED COLLECTIONS Each element identified by a unique index Successive elements have consecutive indices 23

TABLES Each element identified by a unique object called a key Usually strings are

TABLES Each element identified by a unique object called a key Usually strings are used as keys 24

HASHMAP (IMPLEMENTS MAP) // associates key with value, returning last value associated with key

HASHMAP (IMPLEMENTS MAP) // associates key with value, returning last value associated with key public final Object put (Object key, Object value); // returns last value associated with key, or null if no association public final Object get (Object key); Final means method cannot be overridden 25

HASHMAP USE public static void main (String[] args) { Map a. Map = new

HASHMAP USE public static void main (String[] args) { Map a. Map = new Hash. Map(); a. Map. put("Nadal", 10); a. Map. put("Federer", 17); a. Map. put("Sampras", 14); System. out. println(a. Map. get("Nadal")); System. out. println(a. Map. get("nadal")); a. Map. put("Nadal", 11); System. out. println(a. Map. get("Nadal")); System. out. println(a. Map); } 26

EXPLICIT VS. IMPLICIT ELEMENT REFERENCE Collection: Program fragment can refer to any component of

EXPLICIT VS. IMPLICIT ELEMENT REFERENCE Collection: Program fragment can refer to any component of the composite object Indexed collection: components referred explicitly by int expressions Table collection: components referred explicitly by Object expressions Implicit reference to components? 27

STACK: LAST IN FIRST OUT public interface String. Stack { public boolean is. Empty();

STACK: LAST IN FIRST OUT public interface String. Stack { public boolean is. Empty(); public String get. Top(); public void push(String element); public void pop(); } String. Stack string. Stack = new AString. Stack(); string. Stack. push("James Dean"); string. Stack. push("Joe Doe"); string. Stack. push("Jane Smith"); string. Stack. push("John Smith"); System. out. println(string. Stack. get. Top()); string. Stack. pop(); System. out. println(string. Stack. get. Top()); John Smith Jane Smith 28

QUEUE: FIRST IN FIRST OUT public interface String. Queue String. Stack { public boolean

QUEUE: FIRST IN FIRST OUT public interface String. Queue String. Stack { public boolean is. Empty(); public String get. Head(); get. Top(); public void enqueue(String push(String element); public void pop(); dequeue(); } String. Queue string. Q = new AString. Queue(); string. Q. enqueue("James Dean"); string. Q. enqueue("Joe Doe"); string. Q. enqueue("Jane Smith"); string. Q. enqueue("John Smith"); System. out. println(string. Stack. get. Head()); string. Q. dequeue(); System. out. println(string. Stack. get. Head()); James Dean Joe Doe 29

STRUCTURED TYPES Static named Static indexed Dynamic indexed (Static) Dynamic tables Stack (LIFO) Queue

STRUCTURED TYPES Static named Static indexed Dynamic indexed (Static) Dynamic tables Stack (LIFO) Queue (FIFO) 30

READ-ONLY AND EDITABLE PROPERTIES Typed, Named Unit of Exported Class State public class C

READ-ONLY AND EDITABLE PROPERTIES Typed, Named Unit of Exported Class State public class C { Name P Bean Type T public static T get. P() {. . . } Read-only Editable public static void set. P(T new. Value) {. . . } Getter method Setter method } obtain. P Violates Bean convention new. P Bean convention: For humans and tools 31

VISUALIZING COLLECTIONS public interface String. History { public void add. Element(String element); public int

VISUALIZING COLLECTIONS public interface String. History { public void add. Element(String element); public int size(); public String element. At(int index); } public static void main (String[] args) { String. History string. History = new AString. History(); string. History. add. Element("James Dean"); string. History. add. Element("Joe Doe"); string. History. add. Element("Jane Smith"); string. History. add. Element("John Smith"); Object. Editor. edit(string. History); } 32

CONVENTIONS FOR VARIABLE-SIZED COLLECTION Write method (optional) Convention based on Vector @Structure. Pattern(Structure. Pattern.

CONVENTIONS FOR VARIABLE-SIZED COLLECTION Write method (optional) Convention based on Vector @Structure. Pattern(Structure. Pattern. Names. VECTOR_PATTERN) public interface C{ public T element. At (int index); public int size(); public Any set. Element. At(T t, int index); … } Arbitrary Type. Read methods Unconstrained Type (void or T in practice) 33

ALTERNATIVE CONVENTIONS FOR VARIABLE-SIZED COLLECTION Write method (optional) Convention based on Array. List @Structure.

ALTERNATIVE CONVENTIONS FOR VARIABLE-SIZED COLLECTION Write method (optional) Convention based on Array. List @Structure. Pattern(Structure. Pattern. Names. LIST_PATTERN) public interface C { public T get (int index); public int size(); public Any set (int index) T 2); … } Arbitrary Type. Read methods Unconstrained Type (void or T in practice) 34

READ VS. WRITE METHODS Read Methods � Used to get components of object �

READ VS. WRITE METHODS Read Methods � Used to get components of object � Getter methods � size(), element. At() Write Methods � Used to change components of object � Setter methods � add. Element(), remove. Element(), set. Element. At() � some used by Object Editor Distinction independent of conventions and important for Model-View-Controller and other paradigms you will see later Conventions used in Object Editor 35

CONVENTIONS FOR VARIABLE-SIZED COLLECTION Write Method not recognized by OE public interface Point. History

CONVENTIONS FOR VARIABLE-SIZED COLLECTION Write Method not recognized by OE public interface Point. History { public void add. Element (int x, int y); public Point element. At (int index); public int size(); } Read Methods Arbitrary Type 36

APOINTHISTORY Variable-sized Collection History Methods added to menu associated with class Graphic elements of

APOINTHISTORY Variable-sized Collection History Methods added to menu associated with class Graphic elements of dynamic collections added at their (X, Y) locations 37

HASHMAP USE public static void main (String[] args) { Map a. Map = new

HASHMAP USE public static void main (String[] args) { Map a. Map = new Hash. Map(); a. Map. put("Nadal", 10); a. Map. put("Federer", 17); a. Map. put("Sampras", 14); System. out. println(a. Map. get("Nadal")); System. out. println(a. Map. get("nadal")); a. Map. put("Nadal", 11); System. out. println(a. Map. get("Nadal")); System. out. println(a. Map); Object. Editor. edit(a. Map); } 38

OE CONVENTIONS FOR TABLE // associates key with value, returning last value associated with

OE CONVENTIONS FOR TABLE // associates key with value, returning last value associated with key public <Value. Type> put (<Key. Type> key, <Value. Type> value); // returns last value associated with key, or null if no association public <Value. Type> get (<Key. Type> key); // optional, removes associated value, and returns it or null public <Value. Type> remove(<Key. Type> key); Necessary but not sufficient to displays all keys and elements 39

DISPLAYING STACK (LIFO) public interface String. Stack { Does not provide read public boolean

DISPLAYING STACK (LIFO) public interface String. Stack { Does not provide read public boolean is. Empty(); methods for reading all public String get. Top(); elements public void push(String element); public void pop(); } String. Stack string. Stack = new AString. Stack(); string. Stack. push("James Dean"); string. Stack. push("Joe Doe"); string. Stack. push("Jane Smith"); string. Stack. push("John Smith"); Object. Editor. edit(string. Stack); 40

DISPLAYING TRANSPARENT STACK (LIFO) public interface Transparent. String. Stack { public int size(); public

DISPLAYING TRANSPARENT STACK (LIFO) public interface Transparent. String. Stack { public int size(); public String get(int index); public void push(String element); Provides read methods public void pop(); following OE collection } conventions String. Stack string. Stack = new AString. Stack(); Can provide additional string. Stack. push("James Dean"); method for top value string. Stack. push("Joe Doe"); string. Stack. push("Jane Smith"); string. Stack. push("John Smith"); bus. uigen. Object. Editor. edit(string. Stack); 41