Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compsci 201, Collections, Hashing, Objects

Similar presentations


Presentation on theme: "Compsci 201, Collections, Hashing, Objects"— Presentation transcript:

1 Compsci 201, Collections, Hashing, Objects
Owen Astrachan February 1, 2019 2/1/19 Compsci 201, Spring 2019: Collections, Hashing

2 Compsci 201, Spring 2019: Collections, Hashing
G is for … Git Version control that's so au courant Garbage Collection Java recycles Google How to find Stack Overflow 2/1/19 Compsci 201, Spring 2019: Collections, Hashing

3 Compsci 201, Spring 2019: Collections, Hashing
PFFDoSMoY Generic classes: ArrayList to HashSet From ArrayList to HashSet to Collections to … From Object.equals to Object.hashCode Everything is an Object, what can an object do? Using arrays and chars for APT problems Toward understanding maps 2/1/19 Compsci 201, Spring 2019: Collections, Hashing

4 Compsci 201, Spring 2019: Collections, Hashing
WOTO Review Did this very very quickly in class 2/1/19 Compsci 201, Spring 2019: Collections, Hashing

5 Compsci 201, Spring 2019: Collections, Hashing
Diyad ArrayList Review ArrayList is a wrapper-class, array-like behavior but better, e.g., growth when needed Internal state provides array characteristics Methods allow for better functionality 2/1/19 Compsci 201, Spring 2019: Collections, Hashing

6 Diyad ArrayList Growth
When internal array full? Create new, copy, use Efficient add, get, set when done repeatedly Not possible if resize with +1, +100, +1000 Is possible if resize with *2 or *1.25 2/1/19 Compsci 201, Spring 2019: Collections, Hashing

7 Compsci 201, Spring 2019: Collections, Hashing
Generic ArrayList Rather than String, use generic type parameter Can use E, T, Type, any string in <E> Similar to code for GrowableStringArrayList java.util.List Interface 2/1/19 Compsci 201, Spring 2019: Collections, Hashing

8 Can E be anything? String, Point, …
Method .equals that works as expected for E ! Internal array myStorage contains Objects ConformingArrayList<String> What .equals is called? Object or String? Runtime decision, not compile time decision What does elt actually reference? 2/1/19 Compsci 201, Spring 2019: Collections, Hashing

9 Compsci 201, Spring 2019: Collections, Hashing
Why Diyad? Traditionally use ArrayList<E> -- client code Understand methods via API Problem solving in many contexts Efficiency: a.get(1)as fast as a.get(1000) Why efficient? Understanding by analysis From the internal array which is efficient From doubling on resize rather than adding one 2/1/19 Compsci 201, Spring 2019: Collections, Hashing

10 Toward Understanding HashSet
Adding objects to HashSet<..>, avoid duplicates We’ll see with Point class, doesn’t work We’ll see with String class, does work Just as we needed to add .equals() … Need some knowledge of Object and internals of HashSet<..>, how does set.add(X) work? Every object can convert itself to a number Ask not what you can do to an object … 2/1/19 Compsci 201, Spring 2019: Collections, Hashing

11 Compsci 201, Spring 2019: Collections, Hashing
Modified Point Class Why isn't output as expected? Initially why didn't ArrayList work with Point? 2/1/19 Compsci 201, Spring 2019: Collections, Hashing

12 Hollywood Principle: .equals
Why program designers write Point.equals()? To interact well with Java classes We don't call .equals, other classes do We called ArrayList.contains, it called .equals Object has .equals, and .toString Overload these for Point class 2/1/19 Compsci 201, Spring 2019: Collections, Hashing

13 Making .contains efficient
Why is ArrayList.contains(..) slow? Search through entire list to find something If list is sorted can we do better? Think of a number between 1 and 1,024, I'll tell you high, low, correct: how many guesses needed? How do you search for a book in the stacks? That's not what you do in the stacks? What about in ancient times … 2/1/19 Compsci 201, Spring 2019: Collections, Hashing

14 Finding an Object's number ..
Every object has .hashCode() method Returns int value, used as “locker number” Could return 39, 2, 57, … even -321 Cannot guarantee different for every Object! Use .equals in locker Search items in same locker 2/1/19 Compsci 201, Spring 2019: Collections, Hashing

15 Compsci 201, Spring 2019: Collections, Hashing
Ideal world? Real world! 2/1/19 Compsci 201, Spring 2019: Collections, Hashing

16 Compsci 201, Spring 2019: Collections, Hashing
Point.hashCode Convert a Point to a number Try to make every point a different number That's not possible!! For method below, what non-equal points have same .hashCode()? 2/1/19 Compsci 201, Spring 2019: Collections, Hashing

17 Inefficient but Correct .hashCode
Suppose .hashCode()simply returns 5 Every Point goes in the same locker There are always collisions, but we try to minimize them. How are collisions resolved? Can we modify PointDriver.java to stress-test? How many different points can be made? 2/1/19 Compsci 201, Spring 2019: Collections, Hashing

18 Compsci 201, Spring 2019: Collections, Hashing
The hashCode contract Every object has .hashCode() method Inherited from Object, but typically overridden and read online Must respect .equals(): If a.equals(b) ? a.hashCode() == b.hashCode() Converse not true! There will be collisions 2/1/19 Compsci 201, Spring 2019: Collections, Hashing

19 WOTO (correctness counts)
2/1/19 Compsci 201, Spring 2019: Collections, Hashing

20 Compsci 201, Spring 2019: Collections, Hashing
Maria Klawe President of Harvey Mudd Dean of Engineering at Princeton, ACM Fellow, College Dropout (and re-enroller) I personally believe that the most important thing we have to do today is use technology to address societal problems, especially in developing regions Coding is today's language of creativity. All our children deserve a chance to become creators instead consumers of computer science. 2/1/19 Compsci 201, Spring 2019: Collections, Hashing

21 Default: Object.equals, .hashCode
For Objects p and q: p.equals(q) is the same as p == q Do p and q reference/point to same object For Object p p.hashCode() is location in memory of object Thus: if p == q then p.hashCode() == q.hashCode() 2/1/19 Compsci 201, Spring 2019: Collections, Hashing

22 Compsci 201, Spring 2019: Collections, Hashing
ArrayList and HashSet 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() 2/1/19 Compsci 201, Spring 2019: Collections, Hashing

23 Compsci 201, Spring 2019: Collections, Hashing
When Strings Collide 2/1/19 Compsci 201, Spring 2019: Collections, Hashing

24 Compsci 201, Spring 2019: Collections, Hashing
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 2/1/19 Compsci 201, Spring 2019: Collections, Hashing

25 Compsci 201, Spring 2019: Collections, Hashing
Concept: Inheritance In Java, every class extends Object Gets methods by default: .toString, .hashCode, .equals, and more Inherit method + implementation Subclass can override base class methods Make .equals work for Point class 2/1/19 Compsci 201, Spring 2019: Collections, Hashing

26 Compsci 201, Spring 2019: Collections, Hashing
Work in 201 How important are APTs? How important are APT quizzes? How important are assignments? Earlier assignments, later assignments? How important: reading and WOTO in-class How important are reading quizzes? 2/1/19 Compsci 201, Spring 2019: Collections, Hashing

27 Compsci 201, Spring 2019: Collections, Hashing
Alphabetical Order Encryption? Maybe not Think about high-level algorithm Apply your algorithm to: "pop", "array", "deeds" What do we need to do to code algorithm? Recall: 'b' + 1 == 'c' Recall: array['h'] is allowed, 'h' can be index 2/1/19 Compsci 201, Spring 2019: Collections, Hashing

28 Compsci 201, Spring 2019: Collections, Hashing
Ransomware Anonymous headline? High level ideas … Can we create "help me"? Create counts['e'] -- # of e's To use, what's needed 2/1/19 Compsci 201, Spring 2019: Collections, Hashing

29 How often does a string occur?
Strings stored in ArrayList? Call Collections.frequency(list) If in array a rather than ArrayList? Collections.frequency(Arrays.asList(a)) Is this efficient? Does it matter? Can create parallel arrays or use HashMap Keep count[k] # occurrences of word[k] 2/1/19 Compsci 201, Spring 2019: Collections, Hashing

30 WOTO (correctness counts)
2/1/19 Compsci 201, Spring 2019: Collections, Hashing

31 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 Shafi is a serious theoretical computer scientist. Not much to do here but read the bullets and the quote 9/20/17 Compsci 201, Fall 2017, Compare+Analysis


Download ppt "Compsci 201, Collections, Hashing, Objects"

Similar presentations


Ads by Google