Java's Collection Framework: Another Use of Polymorphism Via Interfaces
Java's Collection Framework: Another Use of Polymorphism Via Interfaces
Rick Mercer
1
More useful Polymorphism
2
Outline
4
Abstract Data Type
6
Iterators
Output
1-first
2-second
3-third
7
New way to visit elements:
Java's Enhanced for Loop
w General form
for (Type element : collection) {
element is the next thing visited each iteration
}
8
Polymorphic Algorithms
9
Lower Bound / Upper Bound / Wild Card
Collections.swap(list, 0, 1);
assertEquals("[B, A, C, A]", list.toString());
Collections.sort(list);
assertEquals("[A, A, B, C]", list.toString());
assertEquals(2, Collections.binarySearch(list, "B"));
int index = Collections.binarySearch(list, "A");
assertTrue(index == 0 || index == 1);
Collections.rotate(list, 2);
assertEquals("[B, C, A, A]", list.toString());
11
Set and SortedSet
12
TreeSet elements are in order
names.add("Dakota");
names.add("Devon");
names.add("Chris");
names.add("Chris"); // not added
14
interface Map<K, V>
17
A thread safe FIFO queue
ArrayBlockingQueue<Double> numberQ =
new ArrayBlockingQueue<>(40);
numberQ.add(3.3);
numberQ.add(2.2);
numberQ.add(5.5);
numberQ.add(4.4);
numberQ.add(7.7);
18
http://www.sergiy.ca/guide-to-selecting-appropriate-map-collection-in-java/
19
20
21
There’s more…
22
Interface ListModel
23