forked from matyb/java-koans
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAboutCollections.java
More file actions
133 lines (120 loc) · 3.65 KB
/
AboutCollections.java
File metadata and controls
133 lines (120 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package intermediate;
import static com.sandwich.koan.constant.KoanConstants.__;
import static com.sandwich.util.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.TreeSet;
import com.sandwich.koan.Koan;
public class AboutCollections {
@Koan
public void usingAnArrayList() {
// List = interface
// The generic syntax and special generic cases will be handled in
// AboutGenerics. We just use <String> collections here to keep it
// simple.
List<String> list = new ArrayList<String>();
// ArrayList: simple List implementation
list.add("Chicken");
list.add("Dog");
list.add("Chicken");
assertEquals(list.get(0), __);
assertEquals(list.get(1), __);
assertEquals(list.get(2), __);
}
@Koan
public void usingAQueue() {
// Queue = interface
Queue<String> queue = new PriorityQueue<String>();
// PriorityQueue: simple queue implementation
queue.add("Cat");
queue.add("Dog");
assertEquals(queue.peek(), __);
assertEquals(queue.size(), __);
assertEquals(queue.poll(), __);
assertEquals(queue.size(), __);
assertEquals(queue.poll(), __);
assertEquals(queue.isEmpty(), __);
}
@Koan
public void usingABasicSet() {
Set<String> set = new HashSet<String>();
set.add("Dog");
set.add("Cat");
set.add("Dog");
assertEquals(set.size(), __);
assertEquals(set.contains("Dog"), __);
assertEquals(set.contains("Cat"), __);
assertEquals(set.contains("Chicken"), __);
}
@Koan
public void usingABasicMap() {
Map<String, String> map = new HashMap<String, String>();
map.put("first key", "first value");
map.put("second key", "second value");
map.put("first key", "other value");
assertEquals(map.size(), __);
assertEquals(map.containsKey("first key"), __);
assertEquals(map.containsKey("second key"), __);
assertEquals(map.containsValue("first value"), __);
assertEquals(map.get("first key"), __);
}
@Koan
public void usingBackedArrayList() {
String[] array = {"a","b","c"};
List<String> list = Arrays.asList(array);
list.set(0, "x");
assertEquals(array[0], __);
array[0] = "a";
assertEquals(list.get(0), __);
// Just think of it as quantum state teleportation...
}
@Koan
public void usingBackedSubMap() {
TreeMap<String, String> map = new TreeMap<String, String>();
map.put("a", "Aha");
map.put("b", "Boo");
map.put("c", "Coon");
map.put("e", "Emu");
map.put("f", "Fox");
SortedMap<String, String> backedMap = map.subMap("c", "f");
assertEquals(backedMap.size(), __);
assertEquals(map.size(), __);
backedMap.put("d", "Dog");
assertEquals(backedMap.size(), __);
assertEquals(map.size(), __);
assertEquals(map.containsKey("d"), __);
// Again: backed maps are just like those little quantum states
// that are connected forever...
}
@Koan
public void differenceBetweenOrderedAndSorted() {
TreeSet<String> sorted = new TreeSet<String>();
sorted.add("c");
sorted.add("z");
sorted.add("a");
assertEquals(sorted.first(), __);
assertEquals(sorted.last(), __);
// Look at the different constructors for a TreeSet (or TreeMap)
// Ponder how you might influence the sort order. Hold that thought
// until you approach AboutComparison
LinkedHashSet<String> ordered = new LinkedHashSet<String>();
ordered.add("c");
ordered.add("z");
ordered.add("a");
StringBuffer sb = new StringBuffer();
for(String s: ordered) {
sb.append(s);
}
assertEquals(sb.toString(), __);
}
}