Programming Languages and Techniques (CIS120)

Similar documents
Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120e)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Today. Reading. Homework. Lecture Notes CPSC 224 (Spring 2012) hashcode() method. Collections class. Ch 9: hw 7 out (due in a week)

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

Hash tables. hashing -- idea collision resolution. hash function Java hashcode() for HashMap and HashSet big-o time bounds applications

For this section, we will implement a class with only non-static features, that represents a rectangle

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

More Java Basics. class Vector { Object[] myarray;... //insert x in the array void insert(object x) {...} Then we can use Vector to hold any objects.

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Chapter 11: Collections and Maps

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

Programming Languages and Techniques (CIS120)

Topic 22 Hash Tables

CSE 373. Objects in Collections: Object; equals; compareto; mutability. slides created by Marty Stepp

Domain-Driven Design Activity

Announcements. Container structures so far. IntSet ADT interface. Sets. Today s topic: Hashing (Ch. 10) Next topic: Graphs. Break around 11:45am

Compsci 201 Hashing. Jeff Forbes February 7, /7/18 CompSci 201, Spring 2018, Hashiing

Introducing Hashing. Chapter 21. Copyright 2012 by Pearson Education, Inc. All rights reserved

INSTRUCTIONS TO CANDIDATES

CMSC 132: Object-Oriented Programming II. Hash Tables

Midterm Exam 2 CS 455, Spring 2015

Generics. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 10

Programming Languages and Techniques (CIS120)

The class Object. Lecture CS1122 Summer 2008

Announcements. Today s topic: Hashing (Ch. 10) Next topic: Graphs. Break around 11:45am

Programming Languages and Techniques (CIS120)

Announcements. 1. Forms to return today after class:

JAVA Programming Language Homework II Student ID: Name:

CSCI 136 Data Structures & Advanced Programming. Lecture 12 Fall 2018 Profs Bill & Jon

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

Programming Languages and Techniques (CIS120)

Lecture 18. Collision Resolution

Hash Table. Ric Glassey

Introduction hashing: a technique used for storing and retrieving information as quickly as possible.

Lecture 16: HashTables 10:00 AM, Mar 2, 2018

1.00/1.001 Introduction to Computers and Engineering Problem Solving. Final Exam

MIDTERM EXAM THURSDAY MARCH

CIT-590 Final Exam. Name: Penn Key (Not ID number): If you write a number above, you will lose 1 point

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

CIS 120 Midterm II November 16, Name (printed): Pennkey (login id):

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

The dictionary problem

Hash table basics. ate à. à à mod à 83

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

Hash table basics mod 83 ate. ate. hashcode()

CIS 265 Exam 2 First Name Last Name

COMP 103 RECAP-TODAY. Hashing: collisions. Collisions: open hashing/buckets/chaining. Dealing with Collisions: Two approaches

USAL1J: Java Collections. S. Rosmorduc

Error Handling. public int divide(double a, double b) { if (b==0) return -1; // error double result = a/b; return 0; // success }

Hash table basics mod 83 ate. ate

1.00 Lecture 32. Hashing. Reading for next time: Big Java Motivation

CIS 120 Midterm II November 16, 2012 SOLUTIONS

HASH TABLE BY AKARSH KUMAR

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

CSE 143. Lecture 28: Hashing

Hash table basics mod 83 ate. ate

Programming Languages and Techniques (CIS120)

CS 10: Problem solving via Object Oriented Programming Winter 2017

Collections, Maps and Generics

Public-Service Announcements

CS 520 Theory and Practice of Software Engineering Fall 2017

200 points total. Start early! Update March 27: Problem 2 updated, Problem 8 is now a study problem.

Lecture 10: Introduction to Hash Tables

CS 520 Theory and Practice of Software Engineering Fall 2017

Canonical Form. No argument constructor Object Equality String representation Cloning Serialization Hashing. Software Engineering

CS11 Java. Winter Lecture 8

COE318 Lecture Notes Week 4 (Sept 26, 2011)

EECS2030 Day 8 worksheet Tue Oct 3, // the easy way return Integer.compare(this.size, other.size);

Data Structures and Object-Oriented Design VIII. Spring 2014 Carola Wenk

COMP 250. Lecture 32. interfaces. (Comparable, Iterable & Iterator) Nov. 22/23, 2017

COE318 Lecture Notes Week 3 (Week of Sept 17, 2012)

JAVA Programming Language Homework I - Nested Class

Programming Languages and Techniques (CIS120)

Test Class A. public class A { private int x, y; public int getx() { return x; } public int gety() { return y; }

Final Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Implementing Hash and AVL

COMP 250. Lecture 27. hashing. Nov. 10, 2017

More on Hashing: Collisions. See Chapter 20 of the text.

CS2013 Course Syllabus Spring 2017 Lecture: Friday 8:00 A.M. 9:40 A.M. Lab: Friday 9:40 A.M. 12:00 Noon

Computer Science II (Spring )

COM1020/COM6101: Further Java Programming

Programming Languages and Techniques (CIS120)

Section 05: Midterm Review

1.00/ Introduction to Computers and Engineering Problem Solving. Final Exam / December 21, 2005

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

Randomized Algorithms: Element Distinctness

CSE 143 Sp03 Final Exam Sample Solution Page 1 of 13

CS 520 Theory and Practice of Software Engineering Fall 2018

Section 8: Design Patterns. Slides by Alex Mariakakis. with material from David Mailhot, Hal Perkins, Mike Ernst

Lecture Set 4: More About Methods and More About Operators

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

Transcription:

Programming Languages and Techniques (CIS120) Lecture 37 April 28, 2014 TreeSet and HashSet

Announcements Game project due Wednesday at midnight (HARD deadline) Check your grades online, should be up- to- date Final Exam Wednesday, May 7 th 9-11 AM DRLB A1, Last name A- N DRLB A8, Last name P- Z Old exams posted on the course website for review OH will conznue unzl the exam Review session: Saturday, May 3 rd 6-9PM in Levine 101 Mock exam: Sunday, May 4 th 6-9PM in Levine 101

How is HW10 going so far? 1. not started 2. got an idea, but szll working out the details 3. bugs, bugs, bugs everywhere 4. done and submi_ed!

public class Game { private boolean x; public Game() { boolean x = true; } public boolean foo() { return x; } public static void main(string[] args) { System.out.println(new Game().foo()); } } What gets printed to the console? 1. true 2. false 3. NullPointerExcepZon

CollecZons & User- defined classes

From Wednesday public class Point { private final int x; private final int y; public Point(int x, int y) { this.x = x; this.y = y; } public int getx() { return x; } public int gety() { return y; } } // somewhere in main List<Point> l = new LinkedList<Point>(); l.add(new Point(1,2)); System.out.println(l.contains(new Point(1,2)));! What gets printed to the console? 1. true 2. false

TreeSet public class Point { private final int x; private final int y; public Point(int x, int y) { this.x = x; this.y = y; } public int getx() { return x; } public int gety() { return y; } } // somewhere in main Set<Point> l = new TreeSet<Point>(); l.add(new Point(1,2)); System.out.println(l.contains(new Point(1,2)));! What gets printed to the console? 1. true 2. false 3. ExcepZon in thread "main" java.lang.classcastexcepzon: Point cannot be cast to java.lang.comparable

Search in a BST: (lookup t 8)! 8 > 5 < 5 > 8 > 7 < 1 > > 7 0 3 8 < 9 < 9 8 Lecture 6, slide 15 CIS120

Comparison Operators OCaml s comparison operators (such as < and >) automazcally work for user- defined types type point = { x:int; y:int } ;; print_string (if {x=1;y=2} < {x=2;y=1} then "true" else "false") What gets printed to the console? 1. true 2. false 3. I have no idea Java requires classes to explicitly implement the Comparable<T> interface

Comparable<T> Classes must implement Comparable<T> to be used with TreeSet Returns: interface Comparable<T> { public int compareto(t o); } a negazve number when o is less than this object zero when o is equal to this object a posizve number when o is greater than this object

ImplemenZng compareto public class Point implements Comparable<Point> { private final int x; private final int y; public Point(int x, int y) { this.x = x; this.y = y; } @Override public int compareto(point o) { final int BEFORE = -1; final int EQUAL = 0; final int AFTER = 1; // optimization if (this.x < o.x) return BEFORE; if (this == o) return EQUAL; if (this.x > o.x) return AFTER; if (this.y < o.y) return BEFORE; What is the result of Point(1,2).compareTo(null)? } } if (this.y > o.y) return AFTER; return EQUAL; 1. - 1 2. 0 3. 1 4. NullPointerExcepZon

Contract for compareto An#commu#vity: x.compareto(y) is the opposite of y.compareto(x) (or they are both zero) Excep#on symmetry: x.compareto(y) throws the same excepzons as y.compareto(x)! Transi#vity: if x.compareto(y) > 0 and y.compareto(z) > 0 then x.compareto(z) > 0 (and same for <) Consistency: if x.compareto(y) == 0 then x.compareto(z) returns the same result as y.compareto(z)! Consistency with equals (strongly recommended): x.compareto(y) == 0 if and only if x.equals(y)!

HashSet & HashMap

Clicker quiz public class Point { private final int x; private final int y; public Point(int x, int y) { this.x = x; this.y = y; } public int getx() { return x; } public int gety() { return y; } } // somewhere in main Set<Point> l = new HashSet<Point>(); l.add(new Point(1,2)); System.out.println(l.contains(new Point(1,2)));! What gets printed to the console? 1. true 2. false 3. I have no idea

Hash Maps: The Big Idea Combine: the advantage of arrays: efficient random access to its elements with the advantage of a map datastructure arbitrary keys (not just integers!) How? Create an index into an array by hashing the data in the key to turn it into an int The hashcode method of the Object class does this Resize the array as the number of stored elements grows

Hash Maps, Pictorially Keys hashcode() % array size Array Values John Doe 000 null 001 CSCI Jimmy Bob 002 null 003 CBE Jane Smith 253 DMD Joan Jones 254 255 null WUNG A schemazc HashMap taking Strings (student names) to Undergraduate Majors. Here, John Doe.hashCode() returns an integer n, its hash, such that n mod 256 is 254.

Hash Collisions Uh Oh: Indices derived via hashing may not be unique! Jane Smith.hashCode() % 256 253! Joe Schmoe.hashCode() % 256 253! Good hashcode funczons make it unlikely that two keys will produce the same hash But, it can happen that two keys do produce the same index that is, their hashes collide

BuckeZng and Collisions Using an array of buckets Each bucket stores the mappings for keys that have the same hash. Each bucket is itself a map from keys to values (implemented by a linked list). The buckets can t use hashing to index the values instead they use key equality (via the key s equals method) To lookup a key in the Hash Map: First, find the right bucket by indexing the array through the key s hash Second, search through the bucket to find the value associated with the key Not the only soluzon to the collision problem

BuckeZng Keys hashcode() % array size Array Buckets of Bindings John Doe 000 null 001 Jimmy Bob CSCI Jimmy Bob 002 null 003 Joan Jones CBE Jane Smith 253 Jane Smith Joe Shmoe DMD MATH Joan Jones 254 255 null John Doe WUNG Joe Schmoe Here, Jane Smith.hashCode() and Joe Schmoe.hashCode() happen to collide. The bucket at the corresponding index of the Hash Map array stores the map data.

Hash Map Performance Hashtables are efficient implementazons of Maps and Sets There are many different strategies for dealing with hash collisions with various Zme/space tradeoffs Real implementazons also dynamically resize of the array (which might require re- compuzng the bucket contents) If the hashcode funczon gives a good (close to uniform) distribuzon of hashes the buckets are expected to be small (only one or two elements) Whenever you override equals you must also override hashcode in a consistent way: whenever o1.equals(o2)== true you must ensure that o1.hashcode() == o2.hashcode() Why? Because comparing note: the converse does not have to hold:! hashes is supposed to be a quick approximazon for equality.

Example for Point public class Point { @Override public int hashcode() { final int prime = 31; int result = 1; result = prime * result + x; result = prime * result + y; return result; } }! Examples: (new Point(1,2)).hashCode() yields 994 (new Point(2,1)).hashCode() yields 1024 Note that equal points have the same hashcode Why 31? Prime chosen to create more uniform distribuzon Note: eclipse can generate this code

CompuZng Hashes What is a good recipe for compuzng hash values for your own classes? intuizon: smear the data throughout all the bits of the resulzng integer 1. Start with some constant, arbitrary, non- zero int in result.! 2. For each significant field f of the class (i.e. each field taken into account when compuzng equals), compute a sub hash code c for the field: For boolean fields: (f? 1 : 0)! For byte, char, int, short: (int) f! For long: (int) (f ^ (f >>> 32))! For references: 0 if the reference is null, otherwise use the hashcode() of the field. 3. Accumulate those subhashes into the result by doing (for each field s c): result = prime * result + c;! 4. return result!