Compsci 201, Compare+Analysis

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Sets and Maps Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CSE 373 Data Structures and Algorithms Lecture 18: Hashing III.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Chapter 19 Java Data Structures
Java Collections Framework A presentation by Eric Fabricant.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Not overriding equals  what happens if you do not override equals for a value type class?  all of the Java collections will fail in confusing ways 1.
Copyright © 2002, Systems and Computer Engineering, Carleton University Hashtable.ppt * Object-Oriented Software Development Unit 8.
Chapter 18 Java Collections Framework
1 Collections Framework A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain:
HashCode() 1  if you override equals() you must override hashCode()  otherwise, the hashed containers won't work properly  recall that we did not override.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2011 Space vs. time  space/time tradeoffs  hashing  hash table, hash function  linear probing.
Polymorphism Liskov 8. Outline equals() Revisiting Liskov’s mutable vs. not rule Polymorphism Uniform methods for different types “easy” polymorphism.
Java Type System and Object Model Horstmann ch , 7.7.
1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline  Organizing classes  Design guidelines  Canonical forms of classes equals method hashCode.
CPS APTs and structuring data/information l Is an element in an array, Where is an element in an array?  DIY: use a loop  Use Collections, several.
Compsci 101.2, Fall Plan for eleven-four l Thinking about APTs and test problems  How do you choose: list, string, set, dictionary  Experience?
1 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
CompSci 100 Prog Design and Analysis II Sept 14, 2010 Prof. Rodger CompSci 100, Fall
Interfaces. In order to work with a class, you need to understand the public methods  methods, return types,…  after you instantiate, what can you do.
Collections Dwight Deugo Nesa Matic
CSE 373: Data Structures and Algorithms Lecture 16: Hashing III 1.
CPS What's in Compsci 100? l Understanding tradeoffs: reasoning, analyzing, describing…  Algorithms  Data Structures  Programming  Design l.
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
Building Java Programs Generics, hashing reading: 18.1.
CompSci 101 Introduction to Computer Science March 29, 2016 Prof. Rodger.
CompSci Toward understanding data structures  What can be put in a TreeSet?  What can be sorted?  Where do we find this information?  How do.
Chapter 21 Sets and Maps Jung Soo (Sue) Lim Cal State LA.
Jeff Forbes Owen Astrachan September 8, 2017
Using the Java Collection Libraries COMP 103 # T2
CompSci 101 Introduction to Computer Science
Chapter 19 Java Data Structures
Wednesday Notecards. Wednesday Notecards Wednesday Notecards.
The hashCode method.
Compsci 201, Analysis+Markov
Compsci 201 Priority Queues & Autocomplete
Efficiency add remove find unsorted array O(1) O(n) sorted array
Week 2: 10/1-10/5 Monday Tuesday Wednesday Thursday Friday
Road Map CS Concepts Data Structures Java Language Java Collections
Java Collections Overview
CS313D: Advanced Programming Language
Building Java Programs
CSE 373: Data Structures and Algorithms
CompSci 101 Introduction to Computer Science
CSE 1030: Implementing Non-Static Features
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
<INSERT_WITTY_QUOTE_HERE>
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
slides created by Marty Stepp
Compsci 201 Binary Trees Recurrence Relations
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
Compsci 201, Collections, Hashing, Objects
CSE 143 Lecture 23 Inheritance and the Object class; Polymorphism
CMPE212 – Reminders Assignment 2 due next Friday.
Compsci 201, O-Notation and Maps (Interfaces too)
Introduction to Java Collection
Presentation transcript:

Compsci 201, Compare+Analysis Owen Astrachan Jeff Forbes September 20, 2017 9/20/17 Compsci 201, Fall 2017, Compare+Analysis

Compsci 201, Fall 2017, Compare+Analysis G is for … Garbage Collection Nice to call new and not call delete! Git Version control that's so au courant GPL First open source license Google How to find Stack Overflow 9/20/17 Compsci 201, Fall 2017, Compare+Analysis

Review Pre-class Thinking Think of this as reading, but work to answer questions rather than simply reading code Post questions to Piazza as needed 9/20/17 Compsci 201, Fall 2017, Compare+Analysis

String and Object .hashCode Every object has a .hashCode method Default version? Why does it work for Objects? http://bit.ly/201fall17-object x.equals(y) x.hashCode() == y.hashCode() Why do most classes override both .equals and .hashCode? Correctness and Performance 9/20/17 Compsci 201, Fall 2017, Compare+Analysis

Compsci 201, Fall 2017, Compare+Analysis http://bit.ly/javastring What are instance variables? Initialized? Index used so hash of “cat” != “act” public int hashCode() { int h = hash; if (h == 0 && value.length > 0) { char val[] = value; for (int i = 0; i < value.length; i++) { h = 31 * h + val[i]; } hash = h; return h; 9/20/17 Compsci 201, Fall 2017, Compare+Analysis

Compsci 201, Fall 2017, Compare+Analysis http://bit.ly/javastring How do we tell if two strings are equal? What about a String and an Object? Examine characters at index k in s1 and s2 If not equals, done, return false How many chars to examine? How do we make code easier to read than what’s given in String.java? 9/20/17 Compsci 201, Fall 2017, Compare+Analysis

Compsci 201, Fall 2017, Compare+Analysis Is this code the same? See while loop in http://bit.ly/javastring public boolean equals(Object o) { if (this == o) return true; if (! (o instanceof String)) return false; if (value.length != o.value.length) return false; for(int k=0; k < value.length; k++){ if (value[k] != o.value[k]) return false; } return true; 9/20/17 Compsci 201, Fall 2017, Compare+Analysis

Compsci 201, Fall 2017, Compare+Analysis When Strings Collide https://www.youtube.com/watch?v=HeTShE2PiQI 9/20/17 Compsci 201, Fall 2017, Compare+Analysis

Compsci 201, Fall 2017, Compare+Analysis When Strings Collide Generate strings that will collide Find such strings in the wild String hashCode ayay 3009136 ayBZ bZay bZbZ String hashCode buzzards -931102253 righto snitz 109586548 unprecludible 9/20/17 Compsci 201, Fall 2017, Compare+Analysis

Compsci 201, Fall 2017, Compare+Analysis WOTO http://bit.ly/201fall17-sept20-1 9/20/17 Compsci 201, Fall 2017, Compare+Analysis

Compsci 201, Fall 2017, Compare+Analysis Finishing Last Week DIY SimpleHashSet using an internal hash table ArrayList of buckets/lockers, indexable Find the “right” bucket using .hashCode Internally the bucket stores ArrayList How did we analyze what happens? Create a StringWrapper class that we can instrument Why can’t we instrument String.java? 9/20/17 Compsci 201, Fall 2017, Compare+Analysis

Interplay of .hashCode/.equals Use StringWrapper class to help understand Static variables that count calls of methods When is .hashCode called? When is .equals called? What is interplay between these? Change size of hash table and see what happens! Which calls change, why do they change? 9/20/17 Compsci 201, Fall 2017, Compare+Analysis

Thinking about Hashing http://bit.ly/201fall17-sept15-1 What does StringWrapper help us understand? 9/20/17 Compsci 201, Fall 2017, Compare+Analysis

Compsci 201, Fall 2017, Compare+Analysis Comparing and Sorting Arrays.sort, Collections.sort {“ant”, “bat”, “cat”, “dog”} What algorithm is used in sorting? How to change to sort-in-reverse or other order 9/20/17 Compsci 201, Fall 2017, Compare+Analysis

Strings are Comparable Compare strings lexicographically, natural ordering, dictionary order “zebra” > “aardvark” but “Zebra” < “aardvark” Conceptual, cannot use < or > or == “yak”.compareTo(s) returns < 0, == 0, > 0 s is “zebra”, “yak”, and “toad”, respectively The int convention also used in C++, C, others 9/20/17 Compsci 201, Fall 2017, Compare+Analysis

Not Everything is Comparable 9/20/17 Compsci 201, Fall 2017, Compare+Analysis

Compsci 201, Fall 2017, Compare+Analysis (x,y) < (z,w) Can we compare Point objects? http://stackoverflow.com/questions/5178092/sorting-a-list-of-points-with-java https://stackoverflow.com/questions/6886836/can-i-use-the-operator-to-compare-point-objects-in-java To be “comparable”, implement the Comparable interface, supply .compareTo(..) method 9/20/17 Compsci 201, Fall 2017, Compare+Analysis

Compsci 201, Fall 2017, Compare+Analysis Build on What You Know How does .equals work? Make sure you have the correct type Cast, compare public boolean equals(Object o) { if (o == null || ! (o instanceof Point)) { return false; } Point p = (Point) o; return p.x == x && p.y == y; 9/20/17 Compsci 201, Fall 2017, Compare+Analysis

Compsci 201, Fall 2017, Compare+Analysis Extend what you know This is method in Point class Point implements Comparable<Point> public int compareTo(Point p) { if (this.x < p.x) return -1; if (this.x > p.x) return 1; if (this.y < p.y) return -1; if (this.y > p.y) return 1 return 0; } 9/20/17 Compsci 201, Fall 2017, Compare+Analysis

What is a Java Interface? An enforceable abstraction: methods required Set and Map interfaces Comparable interface If Set<String> is parameter then can pass … HashSet<String> or TreeSet<String> Can sort String or anything that’s Comparable Call .compareTo(..) method 9/20/17 Compsci 201, Fall 2017, Compare+Analysis

Compsci 201, Fall 2017, Compare+Analysis Shafi Goldwasser 2012 Turing Award Winner RCS professor of computer science at MIT Twice Godel Prize winner Grace Murray Hopper Award National Academy Co-inventor of zero-knowledge proof protocols Work on what you like, what feels right, I now of no other way to end up doing creative work 9/20/17 Compsci 201, Fall 2017, Compare+Analysis

Compsci 201, Fall 2017, Compare+Analysis Why use an interface? 9/20/17 Compsci 201, Fall 2017, Compare+Analysis

Compsci 201, Fall 2017, Compare+Analysis Why use an Interface? Work with frameworks, e.g., java.util.Collection Iterable, Serializable, and more – use with Java ArrayList, LinkedList, TreeSet, HashSet all … .clear(), .contains(o), .addAll(..), .size(), … .toArray() https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html 9/20/17 Compsci 201, Fall 2017, Compare+Analysis

Compsci 201, Fall 2017, Compare+Analysis Developer Time Making changes in timeSet and timeUtilSet http://bit.ly/201fall17-setcode Change the call, don’t change method: util TreeSet, HashSet implement same interface Change the call, change method signature Same methods: ArraySet, SimpleHashSet, but no common interface explicit 9/20/17 Compsci 201, Fall 2017, Compare+Analysis

Redesign for Efficiency Consider APT anonymous Green solution http://bit.ly/201fall17-anon for each message: If count(message,’a’) <= count(headline,’a’) OK Repeat for ‘b’, ‘c’, …, ‘z’ Rescan the headline text for ‘a’, … ‘z’ AND for every message Is this a concern? 9/20/17 Compsci 201, Fall 2017, Compare+Analysis

Compsci 201, Fall 2017, Compare+Analysis Quantifying Concerns Counting # occurrences of ‘a’ in headlines requires looking at all Hsize characters We do this 26 times, that’s 26* Hsize (why) We do this for M messages, that’s 26*Hsize*M Also 26*Msize to count chars in each message If we scan headline once to create map: Hsize Process M messages is then 26*M + 26Msize Is 26M + Hsize better than 26*Hsize*M ? 9/20/17 Compsci 201, Fall 2017, Compare+Analysis

Using Numbers to Understand Is 26M + Hsize better than 26*Hsize*M ? Suppose we have 100 messages with 1,000 characters in headlines 2600 + 1000 ____ 2,600,000 Change with 10,000 characters in headlines 2600 + 10,000 ___ 26,000,000 9/20/17 Compsci 201, Fall 2017, Compare+Analysis

Compsci 201, Fall 2017, Compare+Analysis Making Maps In discussion saw int[] counters[256] Can use Map<Character,Integer> to for(char ch : str.toCharArray()){ counters[ch] += 1; } for(char ch : str.toCharArray()){ if (! map.containsKey(ch)) map.put(ch,0); map.put(ch, map.get(ch) + 1); } 9/20/17 Compsci 201, Fall 2017, Compare+Analysis