Underneath the Hood with java util l Using

  • Slides: 16
Download presentation
Underneath the Hood with java. util l Using an API. The client of a

Underneath the Hood with java. util l Using an API. The client of a good library Ø Software design and engineering: techniques Ø Architect, engineer, scientist, creator l Do you need to be able to implement: data structures or algorithms? Ø Learning Objectives of course … CS problem Ø Not all languages have Java's libraries Knowledge of implementation facilitates … Ø Debugging can depend on implementation l Compsci 201, Fall 2016 6. 1

Two collections: arraylist and set l Collection, List, Set…. Oh my! Ø l https:

Two collections: arraylist and set l Collection, List, Set…. Oh my! Ø l https: //docs. oracle. com/javase/8/docs/api/java/util/Collection. h tml What is a Java Interface? Ø Specifies names of methods and some behavior, but implementations can differ on how to implement the interface Ø Difference between add to list and add to set? Ø No syntactic difference, but semantic difference Ø Performance differences too Compsci 201, Fall 2016 6. 2

How do you implement Array. List? l We'll look at some example code for

How do you implement Array. List? l We'll look at some example code for three classes https: //git. cs. duke. edu/201 fall 16/buildingarrays/tree/master/src l l Simple. String. Array. List – when out of room? Ø Throw Exception Growable. String. Array. List – when out of room? Make more room and copy old to new space Ø Grow by adding 1 entry? Grow by doubling Conforming. Array. List Ø Toward using with Collections API Compsci 201, Fall 2016 l 6. 3

Start Simple! l https: //git. cs. duke. edu/201 fall 16/buildingarrays/blob/master/src/Simple. String. Array. List. java

Start Simple! l https: //git. cs. duke. edu/201 fall 16/buildingarrays/blob/master/src/Simple. String. Array. List. java l What happens with methods. add and. get ? Ø If index is out of bounds? If no room to add? l Why is MAX_SIZE static? Don't need to have separate copy in every object, shared across all Ø Static means it's for the class, not for the object Compsci 201, Fall 2016 6. 4

Making Array Grow l When out of storage? Grow by creating more Ø What

Making Array Grow l When out of storage? Grow by creating more Ø What if computer runs out of storage? l What's going on in the body of check. Size()? Ø Ø Ø l Why is this method private? What does System. arraycopy do? What happens to the old 'my. Storage' ? Subtle: why not call range. Check in. add(dex, "str") Ø Very subtle! Compsci 201, Fall 2016 6. 5

Performance Analysis l If array grows by +1 when out of storage, starting with

Performance Analysis l If array grows by +1 when out of storage, starting with "room for one" and adding 1024 elements Ø 1 + 2 + 3 + 4 + … + 100 + 101 + … + 1024 = … Ø Total storage for N? k=1 �∑k = N(N+1)/2 Ø Expression related to N is N 2 l If array grows by doubling, how much? Ø 1 + 2 + 4 + 8 + 16 + 32 + … + 512 + 1024 = … Ø Total storage for N? Expression is 2*N – 1 What do these graphs look like? l Compsci 201, Fall 2016 6. 6

Quadratic v Linear is very fast. But it's not fast enough!? ? ? http:

Quadratic v Linear is very fast. But it's not fast enough!? ? ? http: //bit. ly/201 fall 16 -arrays l Compsci 201, Fall 2016 6. 7

Helpful formulae l We always mean base 2 unless otherwise stated Ø What is

Helpful formulae l We always mean base 2 unless otherwise stated Ø What is log(1024)? Ø log(xy) log(2 n) 2(log n) l Sums (also, use sigma notation when possible) k Ø 1 + 2 + 4 + 8 + … + 2 k = 2 k+1 – 1 = n Ø i S i=1 1 + 2 + 3 + … + n = n(n+1)/2 = a + ar 2 + … + arn-1 = a(rn - 1)/(r-1)= Ø Compsci 201, Fall 2016 n-1 ar S i=0 2 S i=0 i i 6. 8

Questions about Array. List http: //bit. ly/201 fall 16 -sept 16 -1 l You'll

Questions about Array. List http: //bit. ly/201 fall 16 -sept 16 -1 l You'll need to look at code in gitlab, that may not support 200+ simultaneous views, …. Scale? Compsci 201, Fall 2016 6. 9

What is a Java Interface ? l Similar to a class, but a specification

What is a Java Interface ? l Similar to a class, but a specification rather than an implementation public class Array. List implements List Ø Ø List is the interface, Array. List is implementation List supplies method signatures, implementing classes supply … implementations? The Set interface is realized by at least three classes Ø Different performance characteristics Ø Some different use-cases, e. g. , order matters 6. 10 Compsci 201, Fall 2016 l

Key Ideas in Hashing l l Every object has its own idea of where

Key Ideas in Hashing l l Every object has its own idea of where it belongs Ø Ask not what you can do to an object, … Ø Where do you belong? What's your number? In locker? A small arraylist, … Ø Why is it small? Compsci 201, Fall 2016 6. 11

Hashing details? l Every Java object has a value, call. hash. Code() Ø Should

Hashing details? l Every Java object has a value, call. hash. Code() Ø Should respect (at least some) fields Ø Must respect. equals() --- if two objects are. equals(), they must have same. hash. Code() Ø Why is it ok for converse to be false? l When in doubt? Convert to string, call. hash. Code() Ø Need. to. String() anyway l Some details? Compsci 201, Fall 2016 6. 12

What is an Array. List of Array. Lists? l Think lockers, and in each

What is an Array. List of Array. Lists? l Think lockers, and in each locker there's a line of cubbies, an Array. List Ø Easy to implement, performance of remove? . . . Ø Searching in a bucket, or locker, that's long … Ø Avoid Array. List, use Linked List (low-level) l Changes in Java 8 to make more efficient Ø Don't use low-level linked lists Ø Do use low-level trees Compsci 201, Fall 2016 6. 13

Simple. Hash. Set v Array. Set l We'll look carefully at interfaces and client

Simple. Hash. Set v Array. Set l We'll look carefully at interfaces and client code Ø What changes when we change implementation in client/driver program? l Analytic peformance on N words with U unique Ø For every word read …. What do you do ? Ø For Array. Set this is …. NU which means … Ø For Hash. Set this is …. Small buckets means: N Ø If buckets aren't small? Disaster! Collisions Compsci 201, Fall 2016 6. 14

Questions about Sets http: //bit. ly/201 fall 16 -sept 16 -2 l Which method

Questions about Sets http: //bit. ly/201 fall 16 -sept 16 -2 l Which method in the Set interface is hardest to implement? Why? Compsci 201, Fall 2016 6. 15

Theory and Practice l In theory writing software is simple, in practice? Ø Deploying

Theory and Practice l In theory writing software is simple, in practice? Ø Deploying new grading/submission system for assignments in 201 l When you write solutions/programs in 201 Ø Don't try for more than an hour when progress is minimal Ø Don't overestimate how much progress you're making Compsci 201, Fall 2016 6. 16