lecture 5 - national university of singaporecs2030/1718-s2/cs2030-lec05.pdflecture 5 generics and...

47

Upload: others

Post on 14-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:
Page 2: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

Lecture 5Generics and Collections

Page 3: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

Midterm Info

• Date: 5 March 2018Monday after recess week

• Time: 1000am - 1130am 90 minutes

• Venue: MPSH M2C

Page 4: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

Midterm Info

• Scope: Lecture 1-6

• Format: MCQ + short questions

• Open Book

Page 5: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

Previously, in cs2030..

Page 6: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

Variance of Types

• Subtype relationship between complex types

• T <: S implies T[] <: S[]

Page 7: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

(concrete)class interface

Page 8: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

(concrete)class interface(abstract)

class

Page 9: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

abstract class PaintedShape { Color fillColor; void fillWith(Color c) { fillColor = c; } : abstract double getArea(); abstract double getPerimeter(); :}

Page 10: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

(concrete)class

(pure)interface

(abstract)class

interfacewith

defaultmethods

Page 11: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

interface Shape { double getArea(); double getPerimeter(); boolean contains(Point p);}

class Circle implements Shape { .. }class Rectangle implements Shape { .. } :

Page 12: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

interface Shape { double getArea(); double getPerimeter(); boolean contains(Point p); boolean excludes(Point p);}

class Circle implements Shape { .. } class Rectangle implements Shape { .. }

Page 13: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

interface Shape { double getArea(); double getPerimeter(); boolean contains(Point p); default boolean excludes(Point p) { return !contains(p); }}

class Circle implements Shape { .. }class Rectangle implements Shape { .. }

😁

Page 14: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

Generics

Page 15: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

class Queue<T> { :}

Queue<Point> q = new Queue<>(4);

Queue<T> is the generic class Queue<Point> is a parameterised type T is the type parameterPoint is the type argument

Page 16: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

Variance of Generics

• T <: S, then

• T<X> <: S<X> (covariant)

• but X<T> and X<S> are invariant

Page 17: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

Queue<?>

Queue<Shape> Queue<Circle>

Page 18: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

Variance of Generics

• T <: S, then

• T<X> <: S<X> (covariant)

• X<T> and X<S> are invariant

• X<T> <: X<? extends S>

• X<S> <: X<? super T>

Page 19: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

Queue<?>

Queue<? extends Shape>

Queue<? extends Circle>Queue<Shape>

Queue<Circle>

Page 20: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

Queue<?>

Queue<? super Circle>

Queue<? super Shape>Queue<Circle>

Queue<Shape>

Page 21: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

Type Erasure

Page 22: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

class Queue<T> { T[] objects;}

class Queue { Object[] objects;}

type erasure

Page 23: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

AY17/18 Sem 2

javac javaQueue<Circle> Queue

input

output

erase typeargument

execute with Java

Virtual Machine

Page 24: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

class A { void foo(Queue<Circle> c) {} void foo(Queue<Point> c) {}}

class A { void foo(Queue c) {} void foo(Queue c) {}}

type erasure

Page 25: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

class Queue<T> { static int x = 1; static T y; // error static T foo(T t) {}; // error}

Page 26: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

class Queue<T> { static int x = 1; static T y; // error static <X> X foo(X t) {}; //👍}

Page 27: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

Queue<Circle>[] twoQs = new Queue<Circle>[2]; twoQs[0] = new Queue<Circle>();twoQs[1] = new Queue<Point>();

Queue[] twoQs = new Queue[2]; twoQs[0] = new Queue();twoQs[1] = new Queue();

type erasure

Page 28: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

Queue<int> q; // errorQueue<Integer> q; // ok

Page 29: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

Queue<Integer> q = new Queue();

Page 30: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

q.enqueue(new Integer(8));q.enqueue(8);

this is NOT a widening type conversionthis is called autoboxing

Page 31: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

int i = q.dequeue();

Auto-unboxing type conversion

Page 32: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

Autoboxing/unboxing is convenient but beware of

performance penalty

Page 33: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

Type Inference

Page 34: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

Queue<Integer> q = new Queue<>();

Queue.foo(new Point(0,4));

Page 35: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

Java Collections

Page 36: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

Collection<E>

Set<E>

List<E>

Queue<E>

Deque<E>Map<K,V>

Page 37: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

public interface Collection<E> extends Iterable<E> { boolean add(E e); boolean contains(Object o); boolean remove(Object o); void clear(); boolean isEmpty(); int size();

Page 38: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

contains(o) and remove(o) uses

equals(o)

Page 39: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

Object[] toArray(); <T> T[] toArray(T[] a);

Page 40: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

boolean addAll(Collection<? extends E> c);boolean containsAll(Collection<?> c);boolean removeAll(Collection<?> c);boolean retainAll(Collection<?> c); :

Page 41: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

public interface Iterator<E> { boolean hasNext(); E next(); :}

Page 42: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

Collection<E>

Set<E>

List<E>

Duplicate: OKOrder: don’t care

Duplicate: OKOrder: important

Duplicate: NoOrder: Not important

Page 43: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

AbstractList<E>

ArrayList<E>

- add(i, e)- set(i, e)- get(i)- remove(i)- sort(cmp)

-

LinkedList<E>

-

Page 44: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

public interface List<E> { : default void sort(Comparator<? super E> c)

}

Page 45: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

AbstractSet<E>

HashSet<E>

-

-

Page 46: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

AbstractMap<K,V>

HashMap<K,V>

-

-

Page 47: Lecture 5 - National University of Singaporecs2030/1718-s2/cs2030-lec05.pdfLecture 5 Generics and Collections Midterm Info • Date: 5 March 2018 Monday after recess week • Time:

ArrayList<E>LinkedList<E>HashSet<E>HashMap<K,V>

and many more..