Course Announcements Stars is due tomorrow. Stars grades should be out by next Monday. Javascript lab out today. How you make interactive webby GUIs. Today we re going to cover a bit of a hodge podge. John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 1 / 1
Scope, Collections & Generics John Jannotti /course/cs0320/www/lectures/ Feb 8, 2018 John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 2 / 1
A little food for thought on Stars Should my program do this obviously correct thing? Yes. What do you do if two stars have the same coordinate? What do you do if two stars have the same name? Do you report more that one error in a CSV? Do you report where an error in CSV occurred? Can you easily you add new commands? Is your command processing reusable? How did you write tests for your command processing? Can you add() to your KdTree? remove()? John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 3 / 1
Dynamic / Static Dispatch 1 c l a s s Person { 2 S t r i n g c o n f e s s ( Object msg ) { r e t u r n msg. t o S t r i n g ( ) ; 3 } 4 c l a s s Spy e x t e n d s Person { 5 S t r i n g c o n f e s s ( S t r i n g msg ) { r e t u r n Never! ; } 6 } 7 Person p = new Person ( ) ; 8 p. c o n f e s s ( The dock, at 11am ) ; 9 Spy s1 = new Spy ( ) ; 0 s1. c o n f e s s ( The a i r p o r t, at 12pm ) ; 1 Person p2 = s1 ; 2 p2. c o n f e s s ( The depot, at 6pm ) ; 3 Spy s3 = new Spy ( ) ; 4 s3. c o n f e s s ( new S t r i n g B u i l d e r ( The stadium, at 3pm ) ) ; John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 4 / 1
equals / hashcode 1 c l a s s L a b e l { 2 p r i v a t e f i n a l S t r i n g t e x t ; 3 p u b l i c L a b e l ( S t r i n g t ) { t e x t = t ; } 4 } 5 Set<Label > seen = new HashSet <>(); 6 seen. add ( new L a b e l ( John ) ) ; 7 seen. c o n t a i n s ( new L a b e l ( John ) ) ; John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 5 / 1
equals / hashcode 1 c l a s s L a b e l { 2 p r i v a t e f i n a l S t r i n g t e x t ; 3 p u b l i c L a b e l ( S t r i n g t ) { t e x t = t ; } 4 p u b l i c boolean e q u a l s ( Object o ) { 5 / assume c o r r e c t, b a s i c a l l y comparing t e x t / 6 } 7 } 8 Set<Label > seen = new HashSet <>(); 9 seen. add ( new L a b e l ( John ) ) ; 0 seen. c o n t a i n s ( new L a b e l ( John ) ) ; John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 6 / 1
Snapshot Diagrams A standard way to draw the execution state of a program. Primitives are draw bare. Objects are shown as circles, labeled by type. Details as needed. John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 7 / 1
Mutability Modifying an object is changing what s in the circle. We show immutable objects with a double border. Changing them is really reassignment. We use a double arrow to show a reference that can t be changed. ( final ). John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 8 / 1
Local and Global Variables 1 c l a s s Payment { 2 p u b l i c d o u b l e v a l u e ; 3 p u b l i c s t a t i c d o u b l e taxrate = 0. 0 5 ; 4 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) { 5 Payment p = new Payment ( ) ; 6 p. v a l u e = 1 0 0 ; 7 taxrate = 0. 0 5 ; 8 System. out. p r i n t l n ( p. v a l u e (1 + taxrate ) ) ; 9 } 0 } John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 9 / 1
Now we can talk about scopes The basics: static methods can see their own local variables, and static fields of their class. instance methods can see their locals, the static fields and the instance fields (through this). In both, variables of inner blocks can t be seen outside. Strive to minimize scope. John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 10 / 1
Now we can talk about scopes The basics: static methods can see their own local variables, and static fields of their class. instance methods can see their locals, the static fields and the instance fields (through this). In both, variables of inner blocks can t be seen outside. Strive to minimize scope. Now the tricky stuff. Inner class, both static and normal. John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 10 / 1
Generics Many abstractions are generic across types. Holders: Optional, ThreadLocal Factory methods: ImmutableList.of(), DB.getObject(Company.class, id) Collections: List<String>, Map<Label, Person> Before Generics, these abstractions took/returned Object. Return values were cast to the (statically known) return type. The compiler couldn t help with mistakes, refactoring. ClassCastException was a common error. John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 11 / 1
Generic comparison 1 / 2 @param words must c o n t a i n o n l y S t r i n g 3 / 4 v o i d swear ( C o l l e c t i o n words ) { 5 f o r ( Object o : words ) { 6 S t r i n g word = ( S t r i n g ) o ; 7 i f ( word. l e n g t h ( ) == 4) 8 System. out. p r i n t l n ( word ) ; 9 } 0 } 1 2 v o i d swear ( C o l l e c t i o n <S t r i n g > words ) { 3 f o r ( S t r i n g word : words ) 4 i f ( word. l e n g t h ( ) == 4) 5 System. out. p r i n t l n ( word ) ; 6 } John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 12 / 1
Writing generic abstractions You ve probably all used generic abstractions. Have you written any? What? Simple things are pretty simple. Complex things can be very, very complex. John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 13 / 1
Pair.java 1 p u b l i c c l a s s Pair <A, B> { 2 p r i v a t e f i n a l A l e f t ; 3 p r i v a t e f i n a l B r i g h t ; 4 5 p u b l i c P a i r (A a, B b ) { 6 l e f t = a ; 7 r i g h t = b ; 8 } 9 0 @Override 1 p u b l i c S t r i n g t o S t r i n g ( ) { 2 r e t u r n ( +l e f t+. +r i g h t+ ) ; 3 } 4 } John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 14 / 1
Type constraints Why isn t this legal? 1 L i s t <Pair <I n t e g e r, S t r i n g >> l s t = 2 I m m u t a b l e L i s t. o f ( new Pair <>(4, john ), 3 new Pair <>(2, mary ), 4 new Pair <>(7, pat ) ) ; 5 C o l l e c t i o n s. s o r t ( a ) ; John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 15 / 1
Type constraints Why isn t this legal? 1 L i s t <Pair <I n t e g e r, S t r i n g >> l s t = 2 I m m u t a b l e L i s t. o f ( new Pair <>(4, john ), 3 new Pair <>(2, mary ), 4 new Pair <>(7, pat ) ) ; 5 C o l l e c t i o n s. s o r t ( a ) ; Because Pair<Integer,String> does not implement Comparable<Pair<Integer,String>> John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 15 / 1
Can we implement Comparable? 1 p u b l i c c l a s s Pair <A, B> { 2 p u b l i c i n t compareto ( Pair <A, B> o t h e r ) { 3... a. compareto ( o t h e r. a )... 4 } 5 } John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 16 / 1
Can we implement Comparable? 1 p u b l i c c l a s s Pair <A, B> { 2 p u b l i c i n t compareto ( Pair <A, B> o t h e r ) { 3... a. compareto ( o t h e r. a )... 4 } 5 } No. The types A & B might not, themselves, be Comparable<A> and Comparable<B> John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 16 / 1
Manipulating generic objects Suppose your printall() handles any Collection. Iterates over the members and calls tostring() What s the signature of printall()? John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 17 / 1
Manipulating generic objects Suppose your printall() handles any Collection. Iterates over the members and calls tostring() What s the signature of printall()? void printall(collection c);? void printall(collection<object> c);? John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 17 / 1
Manipulating generic objects Suppose your printall() handles any Collection. Iterates over the members and calls tostring() What s the signature of printall()? void printall(collection c);? void printall(collection<object> c);? void printall(collection<?> c); John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 17 / 1
Manipulating generic objects Suppose your printall() handles any Collection. Iterates over the members and calls tostring() What s the signature of printall()? void printall(collection c);? void printall(collection<object> c);? void printall(collection<?> c); What if you want to call draw() on a list of any Shapes? draw(list<shape> shapes);? John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 17 / 1
Manipulating generic objects Suppose your printall() handles any Collection. Iterates over the members and calls tostring() What s the signature of printall()? void printall(collection c);? void printall(collection<object> c);? void printall(collection<?> c); What if you want to call draw() on a list of any Shapes? draw(list<shape> shapes);? John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 17 / 1
Manipulating generic objects Suppose your printall() handles any Collection. Iterates over the members and calls tostring() What s the signature of printall()? void printall(collection c);? void printall(collection<object> c);? void printall(collection<?> c); What if you want to call draw() on a list of any Shapes? draw(list<shape> shapes);? Can you pass in a List<Circle>? John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 17 / 1
Manipulating generic objects Suppose your printall() handles any Collection. Iterates over the members and calls tostring() What s the signature of printall()? void printall(collection c);? void printall(collection<object> c);? void printall(collection<?> c); What if you want to call draw() on a list of any Shapes? draw(list<shape> shapes);? Can you pass in a List<Circle>? draw(list<?>);? John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 17 / 1
Manipulating generic objects Suppose your printall() handles any Collection. Iterates over the members and calls tostring() What s the signature of printall()? void printall(collection c);? void printall(collection<object> c);? void printall(collection<?> c); What if you want to call draw() on a list of any Shapes? draw(list<shape> shapes);? Can you pass in a List<Circle>? draw(list<?>);? draw(list<? extends Shape>); John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 17 / 1
Your KDTree You should have a generic KDTree. You ll thank me later. What makes KDTree<T> harder than List<T>? John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 18 / 1
Your KDTree You should have a generic KDTree. You ll thank me later. What makes KDTree<T> harder than List<T>? KDTrees are not completely generic. Items must have positions. John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 18 / 1
Your KDTree You should have a generic KDTree. You ll thank me later. What makes KDTree<T> harder than List<T>? KDTrees are not completely generic. Items must have positions. In 3D space? KD? John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 18 / 1
Your KDTree You should have a generic KDTree. You ll thank me later. What makes KDTree<T> harder than List<T>? KDTrees are not completely generic. Items must have positions. In 3D space? KD? You want a constraint like draw() has. John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 18 / 1
Your KDTree You should have a generic KDTree. You ll thank me later. What makes KDTree<T> harder than List<T>? KDTrees are not completely generic. Items must have positions. In 3D space? KD? You want a constraint like draw() has. If you have internal classes, they probably need the same constraint. John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 18 / 1
KDTree as Collection Consider implementing Collection. Someone else has thought about completeness. You will interoperate with more code. But (it seems like) you need to implement 15 methods! John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 19 / 1
KDTree as Collection Consider implementing Collection. Someone else has thought about completeness. You will interoperate with more code. But (it seems like) you need to implement 15 methods! You can extend AbstractCollection to get a leg up. You ll just need size and iterator. You ought to provide a constructor from Collection. How about add? That iterator is a bit challenging. Allows for (Star s : tree) { } John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 19 / 1
Enhanced For Loop 1 // Old s t y l e 2 s t a t i c v o i d swear ( C o l l e c t i o n <S t r i n g > c ) { 3 f o r ( I t e r a t o r <S t r i n g > i = c. i t e r a t o r ( ) ; i. hasnext ( ) ; ) 4 S t r i n g word = i. next ( ) ; 5 i f ( word. l e n g t h ( ) == 4) 6 System. out. p r i n t l n ( word ) ; 7 } 8 } 9 0 // New s t y l e 1 s t a t i c v o i d swear ( C o l l e c t i o n <S t r i n g > c ) { 2 f o r ( S t r i n g word : c ) 3 i f ( word. l e n g t h ( ) == 4) 4 System. out. p r i n t l n ( word ) ; 5 } John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 20 / 1
How does the new for loop work? It s just syntactic sugar Essentially compiles to the Iterator code. What s required? c.iterator() A new interface: Iterable Key point: c is Iterable, not an Iterator. An Iterator abstracts one iteration, Iterable abstracts the ability to be iterated over. When will you still use the iterator code pattern? When you need access to Iterator.remove() John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 21 / 1
Why should you bother? Java made many things Iterable Arrays Collections or all kinds So why should you bother makeing your types Iterable? You could just return one of the iterable types. And clients would still get to use the nice for-loops. Remember to copy or wrap modifiable objects. John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 22 / 1
Why should you bother? Java made many things Iterable Arrays Collections or all kinds So why should you bother makeing your types Iterable? You could just return one of the iterable types. And clients would still get to use the nice for-loops. Remember to copy or wrap modifiable objects. But consider allowing iteration over a binary tree. Or your KDTree. Why can t we lean on the existing collections? John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 22 / 1
Why should you bother? Java made many things Iterable Arrays Collections or all kinds So why should you bother makeing your types Iterable? You could just return one of the iterable types. And clients would still get to use the nice for-loops. Remember to copy or wrap modifiable objects. But consider allowing iteration over a binary tree. Or your KDTree. Why can t we lean on the existing collections? You don t want to have to copy your (possibly big) tree into another collection for each call. John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 22 / 1
Making BitSet Iterable Java library provides BitSet A compact representation for a set of (small) integers. Uses an array of integers internally. Each 32-bit integer records the presence/absence of 32 integers. But it isn t Iterable How would we make an Iterable BitSet? What primitives do you need BitSet to have? Anything else you would like? How do you structure the classes? John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 23 / 1
Write a test first 1 @Test p u b l i c v o i d t e s t C o n s t r u c t i o n ( ) { 2 I n t S e t s e t = new I n t S e t ( 2 1, 1 0, 1 3 ) ; 3 Set<I n t e g e r > hs = S e t s. newhashset ( 1 3, 1 0, 2 1 ) ; 4 a s s e r t E q u a l s ( hs. s i z e ( ), s e t. c a r d i n a l i t y ( ) ) ; 5 a s s e r t T r u e ( s e t. get ( 2 1 ) ) ; 6 a s s e r t T r u e ( s e t. get ( 1 0 ) ) ; 7 a s s e r t T r u e ( s e t. get ( 1 3 ) ) ; 8 a s s e r t F a l s e ( s e t. get ( 0 ) ) ; 9 a s s e r t F a l s e ( s e t. get ( 1 ) ) ; 0 a s s e r t F a l s e ( s e t. get ( 1 1 ) ) ; 1 2 f o r ( i n t i : s e t ) { 3 a s s e r t T r u e ( hs. c o n t a i n s ( i ) ) ; 4 hs. remove ( i ) ; 5 } 6 a s s e r t T r u e ( hs. isempty ( ) ) ; John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 24 / 1
IntSet, an Iterable BitSet 1 p u b l i c c l a s s I n t S e t e x t e n d s B i t S e t 2 implements I t e r a b l e <I n t e g e r > { 3 p u b l i c I n t S e t ( I n t e g e r... i n t s ) { 4 s u p e r ( ) ; 5 f o r ( i n t i : i n t s ) 6 s e t ( i ) ; 7 } 8 p u b l i c I t e r a t o r <I n t e g e r > i t e r a t o r ( ) { 9 r e t u r n new I t r ( ) ; 0 } 1 p r i v a t e c l a s s I t r implements I t e r a t o r <I n t e g e r > { 2... 3 } 4 } John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 25 / 1
IntSet$Itr 1 p r i v a t e c l a s s I t r implements I t e r a t o r <I n t e g e r > { 2 i n t c u r s o r = 1; 3 p u b l i c boolean hasnext ( ) { 4 r e t u r n n e x t S e t B i t ( c u r s o r +1) >= 0 ; 5 } 6 p u b l i c I n t e g e r next ( ) { 7 i n t b i t = n e x t S e t B i t ( c u r s o r +1); 8 i f ( b i t < 0) 9 throw new NoSuchElementException ( ) ; 0 c u r s o r = b i t ; 1 r e t u r n c u r s o r ; 2 } 3 p u b l i c v o i d remove ( ) { c l e a r ( c u r s o r ) ; } 4 } John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 26 / 1