COMP 110401 COLLECTION KINDS Instructor Prasun Dewan PREREQUISITE

  • Slides: 36
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

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 3

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

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 5

PRINCIPLE OF LEAST PRIVILEGE Do not give a user of some code more rights

PRINCIPLE OF LEAST PRIVILEGE 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 6

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 7

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 8

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 9

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 10

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(11); names. add("Federer"); grand. Slams. add(17); names. add(“Borg"); grand. Slams. add(11); names. add(“Sampras"); grand. Slams. add(14); } } Primitive values converted to corresponding wrapper objects 11

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

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

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

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 Conventions used in Object Editor 15

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 16

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 17

IMPORTANT VARIABLE SIZED COLLECTIONS Variable-sized collections � History Orders elements Allows retrieval, addition but

IMPORTANT VARIABLE SIZED COLLECTIONS Variable-sized collections � History Orders elements Allows retrieval, addition but not replacement or deletion Point History, String History � Database May order elements Allows retrieval, search, addition, insertion, and unconstrained removal String. Database (did not but should have supported insertion) � Sets No duplicates Other collection kinds? 18

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 19

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 20

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

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

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(11); names. add("Federer"); grand. Slams. add(17); names. add("Djokovic"); grand. Slams. add(5); names. add(“Borg"); grand. Slams. add(11); } } What kind of dynamic structure is being simulated? 23

INDEXED COLLECTIONS Each element identified by a unique index Like array indices, collection indices

INDEXED COLLECTIONS Each element identified by a unique index Like array indices, collection indices are consecutive Highest index – Lowest Index = size -1 public interface Transparent. String. Stack { public int size(); public String get(int index); public void push(String element); public void pop(); } 24

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 25

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

HASHMAP (IMPLEMENT 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); 26

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")); Object. Editor. edit(a. Map); } 27

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 28

EXTRA SLIDES 29

EXTRA SLIDES 29

INDEXOF(STRING ELEMENT) index. Of(“James Dean”); size array 4 James Dean Joe Doe index Jane

INDEXOF(STRING ELEMENT) index. Of(“James Dean”); size array 4 James Dean Joe Doe index Jane Smith 0 John Smith 30

VISUALIZATION OF VARIABLE-SIZED COLLECTIONS public interface String. History { public void add. Element(String element);

VISUALIZATION OF VARIABLE-SIZED COLLECTIONS public interface String. History { public void add. Element(String element); public int size(); public String element. At(int index); } 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(); } 31

STRINGDATABASE COMMANDS 32

STRINGDATABASE COMMANDS 32

GENERIC LIST: VECTOR java. util. List strings = new java. util. Vector(); James Dean

GENERIC LIST: VECTOR java. util. List strings = new java. util. Vector(); James Dean strings. add“James Dean”) Joe Doe strings. add(“Joe Doe”) 33

ARRAYLIST (JAVA. UTIL. ARRAYLIST) AND LIST (JAVA. UTILL. IST) java. util. List strings =

ARRAYLIST (JAVA. UTIL. ARRAYLIST) AND LIST (JAVA. UTILL. IST) java. util. List strings = new java. util. Array. List(); James Dean strings. add(“James Dean”) Joe Doe strings. add(“Joe Doe”) 34

VISUALIZATION OF VARIABLE-SIZED COLLECTIONS public class AString. Database implements String. History { public final

VISUALIZATION OF VARIABLE-SIZED COLLECTIONS public class AString. Database implements String. History { public final int MAX_SIZE = 50; String[] contents = new String[MAX_SIZE]; int size = 0; public int size() { return size; } public String element. At (int index) { return contents[index]; } boolean is. Full() { return size == MAX_SIZE; } public void add. Element(String element) { …} public void remove. Element(String element) {…}; public boolean member(String element) {…}; public void clear() {…}; } 35

IMPORTANT VARIABLE SIZED COLLECTIONS Variable-sized collections � History Orders elements Allows retrieval, addition but

IMPORTANT VARIABLE SIZED COLLECTIONS Variable-sized collections � History Orders elements Allows retrieval, addition but not replacement or deletion Point History, String History � Database May order elements Allows retrieval, search, addition, insertion, and unconstrained removal String. Database (did not but should have supported insertion) 36