Generics Slide
Generics Slide
(Java Programming)
Generics in Java
• generics are a facility of generic programming
• these features were added to the Java programming language back in
2004 within version J2SE 5.0
• they were designed to extend Java's type system to allow a type or
method to operate on objects of various types while providing
compile-time type safety
Generics in Java
• in software engineering bugs are simply a fact of life
• but some bugs are easier to detect than others
• there are 2 main types of bugs and errors
• we can not add an Integer to the list because the type can not be
guaranteed – it may be a List<Double>
• we can not add a Double to the list because the type can not be
guaranteed – it may be a List<Integer>
Upper Bounded Wildcards
you can not add an item to a List<? Extends T> because you
can not guarantee what list it is really pointing to. The only thing you can do
for sure is to read the items
Lower Bounded Wildcards
(Java Programming)
Lower Bounded Wildcards
• we may want to use wildcards with supertypes so parent classes
• this is usually useful when we want to insert items into a generic data
structure or collection
you can not read items from a List<? super T> because you
can not guarantee what list it is really pointing to – we can read Objects exclusively.
We can insert subtypes of T into a List<? super T>
Wildcards and Bounded Types
(Java Programming)
Bounded Types and Wildcards
• there is the so-called get and put principle
• use upper bounded wildcard (extends) when you only get values out of
a structure or collection
• use lower bounded wildcard (super) when you only put values into a
structure or collection
TYPE ERASURE
names.add(”Adam”);
names.add(”Kevin”);
names.add(”Emily”);
while(iter.hasNext())
System.out.println(iter.next());
Collections Framework
names.add(”Adam”);
names.add(”Kevin”);
names.add(”Emily”);
Collection
Stack TreeSet
Collections Framework
Map
HashTable
HashMap
LinkedHashMap
SortedMap
TreeMap
List
(Java Programming)
List
• the list interface is an ordered collection that allows us to store and
access items in a sequential manner
• it extends the Collection interface
• Lists can include duplicate elements (while sets can not)
• it gives the user full visibility and control over the ordering of its
elements
Maps Comparison
(Java Programming)
Maps Comparison
• HashMap and LinkedHashMap rely heavily on a one-dimensional
array and a hash-function
• this is why the average-case running time is O(1) that may be reduced
to O(N) because of collisions
• LinkedHashMap uses doubly-linked list data structure in addition to
maintain insertion order
• TreeMap uses balanced binary search tree
• there no collisions at all but running time is O(logN)
Maps Comparison
CHAINING: we store the items in the same bucket (with same indexes) in a
linked list data structure
HashMap and
LinkedHashMap TreeMap
use arrays and hash-functions uses balanced binary
under the hood search trees + maintains order
WE CAN SHOW
A C A E
INTERACTIONS EASILY
WITH DIAGRAMS !!!
B D F D
there are several important
operations on sets such as
SET #1 SET #2 union or intersection
Sets
a common way of
visualizing sets is the
Venn-diagram approaches
WE CAN SHOW
C A E
INTERACTIONS EASILY
B D F WITH DIAGRAMS !!!
WE CAN SHOW
C A E
INTERACTIONS EASILY
B D F WITH DIAGRAMS !!!
WE CAN SHOW
C A E INTERACTIONS EASILY
B D F WITH DIAGRAMS !!!
WE CAN SHOW
C A E INTERACTIONS EASILY
B D F WITH DIAGRAMS !!!
HashSet and
LinkedHashSet TreeSet
use arrays and hash-functions uses balanced binary
under the hood search trees + maintains order