Download presentation
Presentation is loading. Please wait.
1
Compsci 201, Compare+Analysis
Owen Astrachan Jeff Forbes September 20, 2017 9/20/17 Compsci 201, Fall 2017, Compare+Analysis
2
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
3
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
4
String and Object .hashCode
Every object has a .hashCode method Default version? Why does it work for Objects? 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
5
Compsci 201, Fall 2017, Compare+Analysis
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
6
Compsci 201, Fall 2017, Compare+Analysis
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
7
Compsci 201, Fall 2017, Compare+Analysis
Is this code the same? See while loop in 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
8
Compsci 201, Fall 2017, Compare+Analysis
When Strings Collide 9/20/17 Compsci 201, Fall 2017, Compare+Analysis
9
Compsci 201, Fall 2017, Compare+Analysis
When Strings Collide Generate strings that will collide Find such strings in the wild String hashCode ayay ayBZ bZay bZbZ String hashCode buzzards righto snitz unprecludible 9/20/17 Compsci 201, Fall 2017, Compare+Analysis
10
Compsci 201, Fall 2017, Compare+Analysis
WOTO 9/20/17 Compsci 201, Fall 2017, Compare+Analysis
11
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
12
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
13
Thinking about Hashing
What does StringWrapper help us understand? 9/20/17 Compsci 201, Fall 2017, Compare+Analysis
14
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
15
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
16
Not Everything is Comparable
9/20/17 Compsci 201, Fall 2017, Compare+Analysis
17
Compsci 201, Fall 2017, Compare+Analysis
(x,y) < (z,w) Can we compare Point objects? To be “comparable”, implement the Comparable interface, supply .compareTo(..) method 9/20/17 Compsci 201, Fall 2017, Compare+Analysis
18
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
19
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
20
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
21
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
22
Compsci 201, Fall 2017, Compare+Analysis
Why use an interface? 9/20/17 Compsci 201, Fall 2017, Compare+Analysis
23
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() 9/20/17 Compsci 201, Fall 2017, Compare+Analysis
24
Compsci 201, Fall 2017, Compare+Analysis
Developer Time Making changes in timeSet and timeUtilSet 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
25
Redesign for Efficiency
Consider APT anonymous Green solution 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
26
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
27
Using Numbers to Understand
Is 26M + Hsize better than 26*Hsize*M ? Suppose we have 100 messages with 1,000 characters in headlines ____ 2,600,000 Change with 10,000 characters in headlines ,000 ___ 26,000,000 9/20/17 Compsci 201, Fall 2017, Compare+Analysis
28
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.