Skip to content

Commit f8f9eee

Browse files
committed
Added AboutCollections with koans for each basic collection type.
1 parent ae89269 commit f8f9eee

2 files changed

Lines changed: 142 additions & 0 deletions

File tree

‎koans/src/PathToEnlightment.xml‎

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

0 commit comments

Comments
 (0)