Recap. List Types. List Functionality. ListIterator. Adapter Design Pattern. Department of Computer Science 1

Similar documents
Topic #9: Collections. Readings and References. Collections. Collection Interface. Java Collections CSE142 A-1

CONTAİNERS COLLECTİONS

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University

Java Collections. Readings and References. Collections Framework. Java 2 Collections. References. CSE 403, Winter 2003 Software Engineering

11-1. Collections. CSE 143 Java. Java 2 Collection Interfaces. Goals for Next Several Lectures

Java Collections. Readings and References. Collections Framework. Java 2 Collections. CSE 403, Spring 2004 Software Engineering

The Collections API. Lecture Objectives. The Collections API. Mark Allen Weiss

Type Parameters: E - the type of elements returned by this iterator Methods Modifier and Type Method and Description

Collections class Comparable and Comparator. Slides by Mark Hancock (adapted from notes by Craig Schock)

Lecture 15 Summary. Collections Framework. Collections class Comparable and Comparator. Iterable, Collections List, Set Map

A simple map: Hashtable

Lists and Iterators. CSSE 221 Fundamentals of Software Development Honors Rose-Hulman Institute of Technology

Arrays organize each data element as sequential memory cells each accessed by an index. data data data data data data data data

EXAMINATIONS 2016 TRIMESTER 2

Class 32: The Java Collections Framework

CSC 1214: Object-Oriented Programming

Collections Questions

Lecture 15 Summary 3/11/2009. By the end of this lecture, you will be able to use different types of Collections and Maps in your Java code.

Collections Framework: Part 2

What is the Java Collections Framework?

Generics Collection Framework

27/04/2012. Objectives. Collection. Collections Framework. "Collection" Interface. Collection algorithm. Legacy collection

40) Class can be inherited and instantiated with the package 41) Can be accessible anywhere in the package and only up to sub classes outside the

An Introduction to Data Structures

1.00/ Introduction to Computers and Engineering Problem Solving. Final / December 13, 2004

Review. CSE 143 Java. A Magical Strategy. Hash Function Example. Want to implement Sets of objects Want fast contains( ), add( )

CSC 321: Data Structures. Fall 2013

COURSE 4 PROGRAMMING III OOP. JAVA LANGUAGE

COMP6700/2140 Abstract Data Types: Queue, Set, Map

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 7. (A subset of) the Collections Interface. The Java Collections Interfaces

Java Collections Framework reloaded

CSC 321: Data Structures. Fall 2012

Collections (Java) Collections Framework

Java Collections. Wrapper classes. Wrapper classes

Java Collections. Engi Hafez Seliem

CS Programming Language Java. Fall 2004 Sept. 29

CSC 1351: Final. The code compiles, but when it runs it throws a ArrayIndexOutOfBoundsException

Collections, Maps and Generics

Generics. IRS W-9 Form

Model Solutions. COMP 103: Test April, 2013

Collections. The Java Collections Framework. The Interfaces

CS61BL Summer 2013 Midterm 2

9/16/2010 CS Ananda Gunawardena

CS Ananda Gunawardena

EXAMINATIONS 2017 TRIMESTER 2

CSE 143 Au03 Final Exam Page 1 of 15

Lecture 6 Collections

1.00/ Introduction to Computers and Engineering Problem Solving. Final / December 13, 2004

Overview of Java ArrayList, HashTable, HashMap, Hashet,LinkedList

EXAMINATIONS 2005 END-YEAR. COMP 103 Introduction to Data Structures and Algorithms

182 review 1. Course Goals

COMP 103 Introduction to Data Structures and Algorithms

CS11 Java. Winter Lecture 8

Generics and collections

Object-Oriented Programming with Java

Model Solutions. COMP 103: Mid-term Test. 19th of August, 2016

Lecture 4. The Java Collections Framework

Java Collections Framework: Interfaces

SUMMARY INTRODUCTION COLLECTIONS FRAMEWORK. Introduction Collections and iterators Linked list Array list Hash set Tree set Maps Collections framework

CS211 Computers and Programming Matthew Harris and Alexa Sharp July 9, Boggle

The Java Collections Framework. Chapters 7.5

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

Model Solutions. COMP 103: Test May, 2013

Notes on access restrictions

DM550 Introduction to Programming part 2. Jan Baumbach.

Model Solutions. COMP 103: Mid-term Test. 21st of August, 2014

17. Java Collections. Organizing Data. Generic List in Java: java.util.list. Type Parameters ( Parameteric Polymorphism ) Data Structures that we know

Data Structures. COMS W1007 Introduction to Computer Science. Christopher Conway 1 July 2003

About this exam review

Generic classes & the Java Collections Framework. *Really* Reusable Code

Lists. The List ADT. Reading: Textbook Sections

Computer Science II (Spring )

Doubly LinkedList is Symmetrical! LinkedList Efficiency. Monday, April 8, 13. insert insert remove remove remove walk

Programmieren II. Collections. Alexander Fraser. May 28, (Based on material from T. Bögel)

EXAMINATIONS 2012 Trimester 1, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

Sets and Maps. Part of the Collections Framework

COMP 250 Midterm #2 March 11 th 2013

CSCI 136 Data Structures & Advanced Programming. Lecture 13 Fall 2018 Instructors: Bill 2

(f) Given what we know about linked lists and arrays, when would we choose to use one data structure over the other?

USAL1J: Java Collections. S. Rosmorduc

Fall 2017 Mentoring 9: October 23, Min-Heapify This. Level order, bubbling up. Level order, bubbling down. Reverse level order, bubbling up

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

36. Collections. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Class 26: Linked Lists

CSE 143 SAMPLE MIDTERM

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

Standard ADTs. Lecture 19 CS2110 Summer 2009

Data Structures - CSCI 102. CS102 Hash Tables. Prof. Tejada. Copyright Sheila Tejada

+ Abstract Data Types

Collections (Collection Framework) Sang Shin Java Technology Architect Sun Microsystems, Inc.

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations

Question 0. (1 point) Write the correct ID of the section you normally attend on the cover page of this exam if you have not already done so.

Data Structures and Abstractions with Java

COMP200 GENERICS. OOP using Java, from slides by Shayan Javed

CS 314 Final Fall 2012

Collection Framework Collection, Set, Queue, List, Map 3

Advanced Programming Generics Collections

Interfaces & Generics

EXAMINATIONS 2012 MID YEAR. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

Transcription:

COMP209 Object Oriented Programming Container Classes 3 Mark Hall List Functionality Types List Iterator Adapter design pattern Adapting a LinkedList to a Stack/Queue Map Functionality Hashing Performance Legacy Containers Department of Computer Science 1 Recap So far we ve seen that for holding objects we can use: Arrays provide optimal performance, but are static in size Containers are more sophisticated and flexible, but have various power/performance trade-offs Last time we looked at the functionality present in the Collection interface Also saw that Sets are Collections that hold only one instance of each object value Specs for the add method in Collection are carefully worded to allow for Sets Department of Computer Science 2 List Functionality Most commonly used methods in List add(int index, Object o) insert at the specified position set(int index, Object o) replace at the specified position get(int index) return the object at specified position iterator() get an iterator to the sequence Other useful methods: indexof(object o) return the index of the first occurrence of the element or -1 sublist(int fromindex, int toindex) return a view of this list from fromindex (inclusive) to toindex (exclusive) listiterator() get a ListIterator for this list List Types There are two concrete implementations of List ArrayList Excels at randomly accessing elements Slow when inserting and removing elements from the middle of the List LinkedList Provides optimal sequential access (slow for random access) Inexpensive insertions and deletions from the middle of the List Also has addfirst, addlast, getfirst, getlast, removefirst and removelast methods Allow LinkedList to be used as a stack, a queue, and a deque ListIterator Extends the functionality defined in Iterator especially for Lists Allows the list to be traversed in either direction with next and previous methods Has an add and set method to modify the list These methods operate with respect to the last element returned by a call to next() or previous(), rather than indexes Note that ListIterator should be used only for traversal of an ArrayList as insertion/deletion is expensive compared to a LinkedList Adapter Design Pattern Since the interface of LinkedList allows for the concepts of stack, queue and deque it is not great in terms of cohesion We can apply the Adapter design pattern and make our own Stack and Queue classes Make use of LinkedList, but adapt its interface to a more conventional interface that clients expect Department of Computer Science 5 Department of Computer Science 6 Department of Computer Science 1

Adapter Design Pattern Adapter is similar to both Proxy and Decorator All three make use of object composition However, as was the case with Proxy compared to Decorator, the intent is different Adaptor converts the interface of a class into another interface clients expect. Adapter lets classes work together that couldn t otherwise because of incompatible interfaces InputStreamReader and OutputStreamWriter apply the Adapter design pattern Provide a bridge (or adapt) between byte streams and character streams Department of Computer Science 7 Adapting LinkedList to a Stack or Queue Stack LIFO (last in, first out) container // Adapt a linked list to a Stack interface using // composition public class Stack { private LinkedList mlist = new LinkedList(); public void push(object o) { mlist.addfirst(o); public Object top() { return mlist.getfirst(); public Object pop() { return mlist.removefirst(); public boolean isempty() { return mlist.isempty(); Adapting LinkedList to a Stack or Queue To have only stack behaviour, inheritance would have been inappropriate here Result in a class with all the rest of the LinkedList methods This was the very mistake made made by the Java 1.0 library designers with the Stack class (legacy class) Java 1.0 Stack extends Vector (another legacy class, superceded in the new libraries by ArrayList) Department of Computer Science 9 Adapting LinkedList to a Stack or Queue Queue FIFO (first in, first out) container // Adapt a linked list to a Queue interface using // composition public class Queue { private LinkedList mlist = new LinkedList(); public void put(object o) { mlist.addfirst(o); public Object get() { return mlist.removelast(); public boolean isempty() { return mlist.isempty(); Map Functionality ArrayList allows you to select from a sequence using a number (i.e. associates numbers to objects) Stack has a different selection criterion: the last thing pushed on the stack A map (or dictionary or associative array) allows you to look up an object by using another object Map Functionality Some methods in Map: put(object key, Object val) adds a value and associates it with a key get(object key) returns the value with the corresponding key (or null if the key is not in the map) containskey(object key), containsvalue(object val) test to see if map contains key/value Department of Computer Science 11 Department of Computer Science 12 Department of Computer Science 2

Map Functionality There are two different types of Map HashMap implementation based on a hash table Provides constant time performance for inserting and locating pairs (key, value) Performance can be adjusted via constructors that allow you to set the capacity and load factor of the hash table TreeMap Maintains pairs in sorted order Guarantees log(n) time performance for basic operations by using a balanced binary tree Keys must implement Comparable or a Comparator must be supplied when constructing a TreeMap Map Example Program to check the randomness of Math.random() Random numbers should be uniformly distributed Generate a bunch of random numbers a count the ones that fall into the various ranges HashMap will help us associate <random num, count> pairs Department of Computer Science 13 Department of Computer Science 14 Map Example class Counter { int mcount = 1; // initialize count to 1 public String tostring() { return +mcount; public class Statistics { public static void main(string [] args) { HashMap hm = new HashMap(); for (int i = 0; i < 10000; i++) { // produce a number between 0 and 20 Integer r = new Integer((int)(Math.random() * 20)); if (hm.containskey(r)) { ((Counter)hm.get(r)).mCount++; else { hm.put(r, new Counter()); System.out.println(hm); Map Example Why use Counter instead of int or Integer to hold values Can t use int because containers can only store objects Can t use Integer because we need to update counts and Integer is an immutable class We can use TreeMap (which is a SortedMap) instead of HashMap in order to print out sorted pairs. SortedMap gives us: Object firstkey()/lastkey() lowest and highest key values SortedMap submap(fromkey, tokey) SortedMap headmap(tokey) SortedMap tailmap(fromkey) Hashing and Hash Codes In the previous example we used Integer as a key for HashMap Library classes (such as Integer and String) work fine as keys in hashed containers because hashcode() and equals() have already been implemented for us To use our own classes as keys we must provide suitable hashcode and equals methods Can t just use hashcode and equals inherited from Object as these use object addresses An object that we use for lookup will not have the same address (and therefore will not hash to the same location) as the one stored in the container Furthermore, equals is used to determine if the lookup key is equal to any in the table again, this won t work for addresses Hashing to a location that already has a value is called A collision mykey.hashcode() int Processed by Container Maps to one location in the hash table Hashing and Hash Codes bucket index equals() is used to determine if the key is in the table Department of Computer Science 3

Hashing and Hash Codes The most important factor in creating a hashcode() is that it produces the same value for a given object regardless of when it is called Otherwise you won t be able to retrieve the objects from a hashing-based container If your hashcode depends on mutable data in the object then the user must be made aware that changing the data will change the key Department of Computer Science 19 Hashing and Hash Codes Don t use unique object info for hashcode (eg. object address), use info that identifies the object in a meaningful way Eg. String uses the contents of the string to compute hashcode: System.out.println( Hello.hashCode()); System.out.println( Hello.hashCode()); // two separate Hello String objects, same // hashcode A good hashcode should result in an even distribution of values for optimal performance Worst case: all hashcodes are equal HashMap or HashSet degenerates to a linked list! HashMap Performance Factors Terminology Capacity the number of buckets (locations) in the table Initial capacity the number of buckets when the table is created HashMap and HashSet have constructors that allow you to specify the initial capacity Size the number of entries currently in the table Department of Computer Science 21 HashMap Performance Factors Terminology (cont.) Load factor size/capacity (empty table = 0, half full = 0.5 etc) A lightly loaded table will have fewer collisions and so is optimal for lookups and insertions, but is slower for traversing with an Iterator HashMap and HashSet have constructors that allow you to specify the load factor When the load factor is reached the container automatically increases the capacity (roughly doubles it) and re-distributes the contents (rehashing High load factors decreases space requirements but increases the lookup cost The default load factor (0.75) is a reasonable tradeoff between time and space costs Department of Computer Science 22 The Collections Class There are a number of useful static utility methods in the Collections class Methods to sort and search Lists have same names and signatures as those in Arrays max/min(collection) produces the maximum or minimum element in the argument using the natural comparison method of objects in the Collection max/min(collection, Comparator) produces the maximum or minimum element in the Collection using the supplied Comparator reverse(list) reverses the elements of a List copy(list dest, List src) shuffle(list l, Random r) randomly permute the list Unsupported Operations The Arrays class has a method (Arrays.asList()) that can turn an array into a List Returns a List that is backed by a fixed size array One upshot of this is that add/remove methods don t work for this particular List! add/remove are optional methods in the interface Can compile code that calls add/remove but get an UnsupportedOperationException at runtime Interface contract is broken! Department of Computer Science 24 Department of Computer Science 4

Unsupported Operations Not as bad as it seems Most methods that take a Collection as an argument only read from that Collection all read methods are not optional UnsupportedOperationException is a rare event For most classes, all operations are supported This approach prevents an explosion of interfaces in the design i.e. we don t end up with tons of different interfaces, each of which describe a variation on the main theme e.g. having a FixedSizeList interface for this scenario Legacy Containers Java 1.0/1.1 containers are still supported in the libraries Kept so that old code that uses these containers does not break Should be avoided in favour of the new containers Vector similar to ArrayList Hashtable very similar to HashMap Stack inherits from Vector rather than adapting Vector Use LinkedList instead Department of Computer Science 25 Department of Computer Science 26 Example: Concordance Concordance compute a list of words from an input file, with each word being followed by the lines on which it appears Example from Budd chapter 19, but updated to use the new container classes Use HashMap to store words and Sets of line numbers i.e. word is the key and Set is the associated value Use TreeSet to keep line numbers in sorted order the: 5 7 8 11 12 13 16 18 19 20 27 of: 5 7 8 13 16 18 19 20 polyphonic: 25 29 goes: 13 much: 22 with: 13 26 28 public class Concordance { private Map mmap = new HashMap(); public void readlines(bufferedreader r) throws IOException { String delims = " \t\n.,!?;:[]{()%$^&@#></\"\'-_+=`~1234567890"; String line; int linenumber = 1; while ((line = r.readline())!= null) { line = line.tolowercase(); StringTokenizer tok = new StringTokenizer(line, delims); while (tok.hasmoretokens()) { String next = tok.nexttoken(); // insert the word into the Map enterword(next, new Integer(lineNumber)); linenumber++; // continued on next slide // continued from previous slide private void enterword(string word, Integer linenum) { // use a Set to hold line numbers Set s = (Set) mmap.get(word); // key in Map? if (s == null) { s = new TreeSet(); // associate the Set with the word mmap.put(word, s); s.add(linenum); // add line number to Set // continued on next slide // continued from previous slide public String tostring() { StringBuffer out = new StringBuffer(); // iterate over the keys (words) in the Map Iterator keyiterator = mmap.keyset().iterator(); while (keyiterator.hasnext()) { String word = (String)keyIterator.next(); Set lineset = (Set)mMap.get(word); out.append(word + ": "); // iterate over the Set of line numbers Iterator lineiterator = lineset.iterator(); while (lineiterator.hasnext()) { out.append(lineiterator.next()+" "); out.append('\n'); return out.tostring(); Department of Computer Science 5

Readings Bruce Eckel s Thinking in Java Chapter 9 (2nd ed) or Chapter 11 (3rd ed) (see course web site for link to free online version) Budd section 15.1 (Adapter design pattern) Budd chapter 19 for legacy containers Department of Computer Science 31 Department of Computer Science 6