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