Glitchy App? Compsci 201 Collections, Hashing, Objects Susan Rodger February 5, 2020 2/5/2020 CompSci 201, Spring 2020 1 2/5/2020 CompSci 201, Spring 2020 2 H is for … Announcements • Assignment P2 out later this week • Hashing • APT-3 due Tues, Feb 4, Extended to Thurs Feb 6 • What better way to have a bucket list? • Last chance to turn in Friday til 11:59pm • Discussion 5 on Feb 10 • Prepare for exam • Hexadecimal • Exam next week, Feb 14 • ABC is 10,11,12 • Base 16 > Base 2? 2/5/2020 CompSci 201, Spring 2020 4 2/5/2020 CompSci 201, Spring 2020 5
Midterm Coming Feb 14 PFWBVDW • How much code have you written with paper and a writing utensil? • Interfaces: List, Set, and Map • Tests should measure what you've practiced • When it makes sense to use general type • Practice writing code on paper! • Empirical and Analytical measures of efficiency • Midterm review and previous tests • Maps: API and Problem Solving • These are the best practice available • Keys and Values • Will practice in Discussion • Big-Oh and O-Notation • Logistics • Building a mathematical formalism with intuition • Start on time, end on time, accommodations • 1 page front and back of notes you bring and leave 2/5/2020 CompSci 201, Spring 2020 6 2/5/2020 CompSci 201, Spring 2020 7 Breakfast 201 was yummy! The hashCode contract • Every object has .hashCode() method • Wed. Feb 5 9:30am • 30 minutes, discuss whatever with me • Inherited from Object, but typically overridden • Use @Override and read online • Enjoy breakfast • More breakfasts comingl… • Must respect .equals(): If a.equals(b) ? • a.hashCode() == b.hashCode() • Converse not true! There will be collisions 2/5/2020 CompSci 201, Spring 2020 8 2/5/2020 CompSci 201, Spring 2020 9
Default: Object.equals, .hashCode When Strings Collide When you do not override… • Generate strings that will collide • For Objects p and q: • p.equals(q) is the same as p == q • Find such strings in the wild • Do p and q reference/point to same object String hashCode String hashCode ayay 3009136 buzzards -931102253 • For Object p • p.hashCode() is location in memory of object ayBZ 3009136 righto -931102253 bZay 3009136 snitz 109586548 • Thus: if p == q then bZbZ 3009136 unprecludible 109586548 • p.hashCode() == q.hashCode() 2/5/2020 CompSci 201, Spring 2020 10 2/5/2020 CompSci 201, Spring 2020 11 Summary: ArrayList and HashSet When Strings Collide • Both have .add, .addAll, and more • Both iterable: for(Elt e : collection) • Both have .contains leveraging .equals • HashSet also uses .hashCode to reduce the collection iterated over: locker collisions • Object hygiene when developing your classes • .toString(), .equals(), .hashCode() https://www.youtube.com/watch?v=HeTShE2PiQI 2/5/2020 CompSci 201, Spring 2020 13 2/5/2020 CompSci 201, Spring 2020 14
When Strings Collide Concept: Inheritance • Generate strings that will collide • In Java, every class extends Object • Find such strings in the wild • Gets methods by default: .toString, .hashCode, .equals, and more String hashCode String hashCode • Inherit method + implementation ayay 3009136 buzzards -931102253 • Subclass can override base class methods ayBZ 3009136 righto -931102253 • Make .equals work for Point class bZay 3009136 snitz 109586548 bZbZ 3009136 unprecludible 109586548 2/5/2020 CompSci 201, Spring 2020 15 2/5/2020 CompSci 201, Spring 2020 16 Work in 201 Alphabetical Order • How important are APTs? • Encryption? Maybe not • https://www2.cs.duke.edu/csed/newapt/encryption.html • How important are APT quizzes? • Think about high-level algorithm • Apply your algorithm to: "pop", "array", "deeds" • How important are assignments? • Earlier assignments, later assignments? • What do we need to do to code algorithm? • Recall: 'b' + 1 == 'c' • How important: reading and WOTO in-class • Recall: array['h'] is allowed, 'h' can be index • How important is reading? 2/5/2020 CompSci 201, Spring 2020 17 2/5/2020 CompSci 201, Spring 2020 18
Idea with Encryption APT How often does a string occur? • Strings stored in ArrayList? • Call int[] allchars = new int[256]; 96 0 Collections.frequency(list,word) int nextLet is 'a' 97 0 ‘a’ • If in array a rather than ArrayList? String answer = ""; 0 98 ‘b’ Collections.frequency(Arrays.asList(a), word) 99 0 ‘c’ ArrayList<String> list is message is feed 100 0 ‘d’ [“cat”, “cat”, “dog”, “fish”, “dog”, “cat”] ‘e’ 101 0 answer is 0 ‘f’ 102 Collections.frequency(list, “dog”) is 103 0 ‘g’ ch is Collections.frequency(list, “cat”) is 2/5/2020 CompSci 201, Spring 2020 19 2/5/2020 CompSci 201, Spring 2020 36 How often does a string occur? WOTO (correctness counts) • Is Collections.frequency efficient? Does it matter? http://bit.ly/201spring20-0205-1 • Use Collections.frequency • Can create parallel arrays or use HashMap • Keep count[k] # occurrences of word[k] • Use HashMap if you know that 2/5/2020 CompSci 201, Spring 2020 38 2/5/2020 CompSci 201, Spring 2020 39
Shafi Goldwasser Why use an interface? • 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 know of no other way to end up doing creative work 2/5/2020 CompSci 201, Spring 2020 40 2/5/2020 CompSci 201, Spring 2020 41 What is a Java Interface? Analogy: Mammals • An enforceable abstraction: methods required • Dragon? • Set, Map, List interfaces • Mammals • Can implement more than one interface • Birth to live young • Can extend only one base-class! • Hair • Warm-blooded • Arguable: Mammal is an interface • Like an interface! • Do NOT inherit method implementations • Do inherit methods (names, types, etc.) 2/5/2020 CompSci 201, Spring 2020 43 2/5/2020 CompSci 201, Spring 2020 44
Why use an Interface? There are two kinds … • There are 10 kinds of people in the world … • Work with frameworks, e.g., java.util.Collection • Those who understand binary and … • Iterable, Serializable, and more – use with Java • Is this funny? • ArrayList, LinkedList, TreeSet, HashSet all … • HashSet/HashMap and TreeSet/TreeMap • .clear(), .contains(o), • Tradeoffs in efficiency, organization .addAll(..) , .size(), … .toArray() • LinkedList/ArrayList https://docs.oracle.com/en/java/javase/11/docs/api/j • Tradeoffs in efficiency, organization ava.base/java/util/Collection.html 2/5/2020 CompSci 201, Spring 2020 46 2/5/2020 CompSci 201, Spring 2020 47 Preliminaries Link v Array • List<..> is an interface in java.util • LinkedList<..> and ArrayList<..> • Getting between two elements • Implement the interface • Unsnap/Snap v Shift/Insert • What is null? • Variable value • No object referenced 2/5/2020 CompSci 201, Spring 2020 48 2/5/2020 CompSci 201, Spring 2020 49
Benchmark: Empirical Analysis list.remove(0) https://coursework.cs.duke.edu/201spring20/classcode/ • What is “faster”? LinkedList or ArrayList • • In class ListSplicer, method removeFirst • List<String> parameter • ArrayList<String> argument passed • LinkedList<String> argument passed • Only call List<..> interface methods • At runtime, call the actual object method • LinkedList.add v ArrayList.add 2/5/2020 CompSci 201, Spring 2020 50 2/5/2020 CompSci 201, Spring 2020 51 list.remove(0) – where list.remove(0) called • What is “faster”? LinkedList or ArrayList RemoveFirst 1.4 y = 0.0064x 2 - 0.0156x + 0.0238 1.2 R² = 0.9984 1 0.8 0.6 0.4 0.2 y = -4E-05x + 0.0009 0 linked array Linear (linked) Poly. (array) 2/5/2020 CompSci 201, Spring 2020 52 2/5/2020 CompSci 201, Spring 2020 53
Access all elements randomly Access all elements randomly • What is “faster”? LinkedList or ArrayList • What is “faster”? LinkedList or ArrayList Random Access 25 y = 0.1292x 2 - 0.7137x + 1.3337 20 R² = 0.9889 15 10 5 y = 0.0002x + 5E-05 R² = 0.8169 0 10000 20000 30000 40000 50000 60000 70000 80000 90000 100000110000120000130000140000150000 linked array Poly. (linked) Linear (array) 2/5/2020 CompSci 201, Spring 2020 54 2/5/2020 CompSci 201, Spring 2020 55
Recommend
More recommend