10-Genericsa 4 in 1 PDF
10-Genericsa 4 in 1 PDF
Java 5
and Systems
Lecture 10/11 : Java Generics and Collections Some features in new language
• Overview boxing/unboxing
• Subtyping and Wildcard
• Comparison and Bounds new form of loop
• Declaration and Erasure
• Reification and Reflection functions with variable number of arguments
• Collections
• Iterator, Iterable, Collection generics
• Set, Queues, List, Maps
• Design Patterns more concurrency features
• Other Issues
type safety
new
optimization (fewer castings) loop
Important Principle :
60% slower
Iterator supports iteration through a collection. Syntactic sugar to support Varargs. varargs
Getting elements :
Contravariant Subtyping :
List<Number> <: List<? super Integer>
list of elements of any type that is a supertype of Number Two Bounds? Not legal though plausible.
Transitivity :
if x.compareTo(y)<0 and y.compareTo(z)<0
then x.compareTo(z)<0
Congruence :
if x.compareTo(y)==0 then A more general signature is based on get/put principle:
forall z. sgn(x.compareTo(z)==sgn(x.compareTo(z))
not reifiable
Java.lang.reflect.array.newInstances(int.class,size)
returns int[] and not Integer[] through a hack!
newArray
Output :
newArray