-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAboutCollections.java
More file actions
121 lines (108 loc) · 3.91 KB
/
AboutCollections.java
File metadata and controls
121 lines (108 loc) · 3.91 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
package intermediate;
import com.sandwich.koan.Koan;
import java.util.*;
import static com.sandwich.koan.constant.KoanConstants.__;
import static com.sandwich.util.Assert.assertEquals;
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), "Chicken");
assertEquals(list.get(1), "Dog");
assertEquals(list.get(2), "Chicken");
}
@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(), "Cat");
assertEquals(queue.size(), 2);
assertEquals(queue.poll(), "Cat");
assertEquals(queue.size(), 1);
assertEquals(queue.poll(), "Dog");
assertEquals(queue.isEmpty(), true);
}
@Koan
public void usingABasicSet() {
Set<String> set = new HashSet<String>();
set.add("Dog");
set.add("Cat");
set.add("Dog");
assertEquals(set.size(), 2);
assertEquals(set.contains("Dog"), true);
assertEquals(set.contains("Cat"), true);
assertEquals(set.contains("Chicken"), false);
}
@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(), 2);
assertEquals(map.containsKey("first key"), true);
assertEquals(map.containsKey("second key"), true);
assertEquals(map.containsValue("first value"), false);
assertEquals(map.get("first key"), "other value");
}
@Koan
public void usingBackedArrayList() {
String[] array = {"a", "b", "c"};
List<String> list = Arrays.asList(array);
list.set(0, "x");
assertEquals(array[0], "x");
array[0] = "a";
assertEquals(list.get(0),"a" );
// 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(), 2);
assertEquals(map.size(), 5);
backedMap.put("d", "Dog");
assertEquals(backedMap.size(), 3);
assertEquals(map.size(), 6);
assertEquals(map.containsKey("d"), true);
// 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(), "a");
assertEquals(sorted.last(), "z");
// 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(), "cza");
}
}