Prof. Christopher Rasmussen
Spring, 2019
CISC 181
Generics & Collections
Generic methods
Generic methods
Generic methods
public static <T>
T tripleMinValue(T item1, T item2, T item3) {
// . . .
}
Generic methods
public static <T>
T tripleMinValue(T item1, T item2, T item3) {
// . . .
}
Generic methods
public static <T1, T2>
T1 foo(T1 item1, T2 item2) {
// . . .
}
Generic methods
Generic methods
public static <T extends Comparable<T>>
T tripleMinValue(T item1, T item2, T item3) {
if (item1.compareTo(item2) <= 0) {
...
}
}
Generic classes: Motivation
class TwoIntegers {
Integer I1, I2;
TwoIntegers(Integer I1, Integer I2)
{ this.I1 = I1; this.I2 = I2; }
}
class TwoStrings {
String S1, S2;
TwoStrings(String S1, String S2)
{ this.S1 = S1; this.S2 = S2; }
}
Generic classes: Motivation
Generic classes: Motivation
class TwoVars {
AnyType V1, V2;
TwoVars(AnyType V1, AnyType V2)
{ this.V1 = V1; this.V2 = V2; }
}
Generic classes: Defining
class TwoVars <AnyType> {
AnyType V1, V2;
TwoVars(AnyType V1, AnyType V2)
{ this.V1 = V1; this.V2 = V2; }
}
Generic classes: Defining
class TwoVars <Var> {
Var V1, V2;
TwoVars(Var V1, Var V2)
{ this.V1 = V1; this.V2 = V2; }
}
Generic classes: Defining
class TwoVars <T> {
T V1, V2;
TwoVars(T V1, T V2)
{ this.V1 = V1; this.V2 = V2; }
}
Generic classes: Defining
class KeyValue <K, V> {
K key;
V value;
KeyValue(K key, V value) {
this.key = key;
this.value = value;
}
}
Generic classes: Defining
class TwoComps <T extends JComponent> {
T C1, C2;
TwoComps(T C1, T C2)
{ this.C1 = C1; this.C2 = C2; }
}
Using generic classes
Using generic classes
TwoVars<Integer> pairOfIntegers;
// this is the old way
pairOfIntegers = new TwoVars<Integer>(3, 5);
Using generic classes
TwoVars<Integer> pairOfIntegers;
// but this is also ok now
pairOfIntegers = new TwoVars<>(3, 5);
Using generic classes
TwoVars<Integer> pairOfIntegers;
// but this is also ok now
pairOfIntegers = new TwoVars<>(3, 5);
Examples of generic classes/interfaces
Examples of generic classes/interfaces
Java Collections vs. Collection
Java Collections vs. Collection
Java Collections vs. Collection
Queue/Deque
Collection examples
Collection examples
Collection examples
Useful Collections methods, part I
Useful Collections methods, part I
Useful Collections methods, part I
Useful Collections methods, part I
Useful Collections methods, part II
Useful Collections methods, part II
Useful Collections methods, part II
Set (Collection interface)
Set (Collection interface)
Set (Collection interface)
Iterators
Iterators
Iterators
Iterators
look familiar? that's because the Scanner class implements Iterator!!
Iterators
// myArrList elements are of type E
Iterator<E> iter = myArrList.iterator();
while (iter.hasNext()) {
E curElem = iter.next();
// do something with curElem
}
Set (Collection interface)
Set (Collection interface)
Set (Collection interface)
Can call constructor with Comparator object to specify custom sorting criterion
Set operations
Set operations
Exercise (codestepbystep.com)
Exercise (codestepbystep.com)
Exercise (codestepbystep.com)