Professional Documents
Culture Documents
3 Collections
3 Collections
Collections Framework
SOFTENG 251
Object Oriented Software Construction
Preamble
Tutorial?
Review of Generics
More on Collections – Lists, Sets, Maps, algorithms
Assignment queries
Go over exercise(s) from Wiki
Friday session – group 8
Multi-choice questions, and/or
Mini-exercise
Polymorphism, generics, collections
Design notes:
Above method takes in an object whose class implements Collection
List, ArrayList, LinkedList, Set, HashSet, TreeSet, Queue, MyOwnCollection, etc
We know any such object can return an Iterator through method iterator()
We don’t know the exact implementation of Iterator we are getting, but we
don’t care, as long as it provides the methods next() and hasNext()
Good practice: Program to an interface!
}
System.out.println(item); = Item item = it.next();
System.out.println(item);
}
What types of objects can you sort? Anything that has an ordering
Two sort() methods: sort a given List according to either 1) natural
ordering of elements or an 2) externally defined ordering.
1) public static <T extends Comparable<? super T>> void sort(List<T> list)
Translation:
1. Only accepts a List parameterised with type implementing Comparable
2. Accepts a List parameterised with any type as long as you also give it a
Comparator implementation that defines the ordering for that type
Collections framework SOFTENG 251 Object Oriented Software Construction 11
java.lang.Comparable<T>
A generic interface with a single method: int compareTo(T)
Return 0 if this = other
Return any +’ve integer if this > other
Return any –’ve integer if this < other
Implement this interface to define natural ordering on objects of type T
public class Money implements Comparable<Money> {
...
public int compareTo( Money other ) { m1 = new Money(100,0);
if( this.cents == other.cents ) { m2 = new Money(50,0);
return 0; m1.compareTo(m2) returns 1;
}
else if( this.cents < other.cents ) {
return -1;
}
else { A more concise way of doing this? (hint: 1 line)
return 1;
} return this.cents – other.cents;
}
public static <T extends Comparable<? super T>> void sort(List<T> list)
Wildcard (later)
Collections framework SOFTENG 251 Object Oriented Software Construction 13
java.util.Comparator<T>
Useful if the type of elements to be sorted is not Comparable, or
you want to define an alternative ordering <<interface>>
Also a generic interface that defines methods Comparator<T>
compare(T,T) and equals(Object) +compare(T o1, T o2):int
+equals(Object other):boolean
Usually only need to define compare(T,T)
Define ordering by CD’s getPrice() Money CD
Note: PriceComparator implements a Comparator para- +getTitle():String
meterised with CD T “becomes” CD +getArtist():String
+getPrice():Money
public class PriceComparator
implements Comparator<CD> {
public int compare(CD c1, CD c2) {
return c1.getPrice().compareTo(c2.getPrice());
}
}
Comparator and Comparable
going hand in hand
contains(e) ? isEmpty()
true a b false <<interface>>
e SortedSet<E>
contains(x) c size() +first():E
false d
5 +last():E
etc…
remove(n) values()
k a keySet() Collection
b <<interface>>
m b Set SortedMap<K,V>
k a
remove(x) p c b
m p +firstKey():K
null c b +lastKey():K
n b n etc…
Collections framework SOFTENG 251 Object Oriented Software Construction 20
HashMap<K,V>
<<interface>>
aka Hashtable (SE250) Map<K,V>
keys are hashed using Object.hashCode() +put(K,V):V
+get(Object):V
i.e. no guaranteed ordering of keys +remove(Object):V
keySet() returns a HashSet +size():int
+keySet():Set<K>
values() returns an unknown Collection +values():Collection<V>
etc…
Map<String, Integer> directory
= new HashMap<String, Integer>();
directory.put("Mum", new Integer(9998888));
directory.put("Dad", 9998888); HashMap<K,V>
directory.put("Bob", 12345678); “autoboxing”
directory.put("Edward", 5553535);
directory.put("Bob", 1000000);
4 or 5?
System.out.println(directory.size());
for (String key : directory.keySet()) {
System.out.print(key+"'s number: "); Set<String>
System.out.println(directory.get(key));
} What’s Bob’s number?
System.out.println(directory.values());
«interface» «interface»
Collection Map
«interface»
«interface» «interface» «interface»
Set List Queue SortedMap
Linked TreeSet
HashSet Implementations