COMP2511
Generics and Collections in Java
Prepared by
Dr. Ashesh Mahidadia
Generics in Java
(Part 1)
2COMP2511: Generics and Collections in Java
Generics in Java
Generics enable types (classes and interfaces) to be parameters when defining:
• classes,
• interfaces and
• methods.
Benefits
v Removes casting and offers stronger type checks at compile time.
v Allows implementations of generic algorithms, that work on collections of different types, can
be customized, and are type safe.
v Adds stability to your code by making more of your bugs detectable at compile time.
COMP2511: Generics and Collections in Java 3
Without Generics With Generics
Generic Types
v A generic type is a generic class or interface that is parameterized over types.
v A generic class is defined with the following format:
class name< T1, T2, ..., Tn > { /* … */ }
v The most commonly used type parameter names are:
v E – Element (used extensively by the Java Collections Framework)
v K – Key
v N – Number
v T – Type
v V – Value
v S,U,V etc. – 2nd, 3rd, 4th types
v For example,
Box
OR
Box
COMP2511: Generics and Collections in Java 4
Multiple Type Parameters
v A generic class can have multiple type
parameters.
v For example, the generic OrderedPair class,
which implements the generic Pair interface
COMP2511: Generics and Collections in Java 5
v Usage examples,
Pair
Pair
… …
OrderedPair
OrderedPair
… …
OrderedPair
Generic Methods
Generic methods are methods that introduce their own type parameters.
COMP2511: Generics and Collections in Java 6
The complete syntax for invoking this method would be:
Pair
Pair
boolean same = Util.
The type has been explicitly provided, as shown above.
Generally, this can be left out and the compiler will infer the type that is needed:
Pair
Pair
boolean same = Util.compare(p1, p2);
Collections in Java
COMP2511: Generics and Collections in Java 7
Collections in Java
A collections framework is a unified architecture for representing and manipulating
collections. A collection is simply an object that groups multiple elements into a single unit.
All collections frameworks contain the following:
v Interfaces: allows collections to be manipulated independently of the details of their
representation.
v Implementations: concrete implementations of the collection interfaces.
v Algorithms: the methods that perform useful computations, such as searching and
sorting, on objects that implement collection interfaces.
• The algorithms are said to be polymorphic: that is, the same method can be used
on many different implementations of the appropriate collection interface.
COMP2511: Generics and Collections in Java 8
Core Collection Interfaces:
v The core collection interfaces encapsulate different types of collections
v The interfaces allow collections to be manipulated independently of the details of their
representation.
COMP2511: Generics and Collections in Java 9
The Collection Interface
v A Collection represents a group of objects known as its elements.
v The Collection interface is used to pass around collections of objects where maximum
generality is desired.
v For example, by convention all general-purpose collection implementations have a
constructor that takes a Collection argument.
v The Collection interface contains methods that perform basic operations, such as
• int size(),
• boolean isEmpty(),
• boolean contains(Object element),
• boolean add(E element),
• boolean remove(Object element),
• Iterator
• … …many more …
More at : https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
COMP2511: Generics and Collections in Java 10
https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
Collection Implementations
v The general purpose implementations are summarized in the following table:
COMP2511: Generics and Collections in Java 11
Interface Hash Table Resizable Array Balanced Tree Linked List Hash Table + Linked List
Set HashSet TreeSet LinkedHashSet
List ArrayList LinkedList
Deque ArrayDeque LinkedList
Map HashMap TreeMap LinkedHashMap
Implemented Classes in the Java Collection,
Read their APIs.
v Overview of the Collections Framework at the following page:
https://docs.oracle.com/javase/8/docs/technotes/guides/collections/overview.html
https://docs.oracle.com/javase/8/docs/api/java/util/HashSet.html
https://docs.oracle.com/javase/8/docs/api/java/util/TreeSet.html
https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashSet.html
https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
https://docs.oracle.com/javase/8/docs/api/java/util/LinkedList.html
https://docs.oracle.com/javase/8/docs/api/java/util/ArrayDeque.html
https://docs.oracle.com/javase/8/docs/api/java/util/LinkedList.html
https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html
https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html
https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html
https://docs.oracle.com/javase/8/docs/technotes/guides/collections/overview.html
Wrappers for the Collection classes
• https://docs.oracle.com/javase/tutorial/collections/implementations/
wrapper.html
COMP2511: Generics and Collections in Java 12
https://docs.oracle.com/javase/tutorial/collections/implementations/wrapper.html
Demo: Collections Framework
Demo …
COMP2511: Generics and Collections in Java 13
End
COMP2511: Generics and Collections in Java 14