intermediate.AboutCollections Maven / Gradle / Ivy
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 collections here to keep it
// simple.
List list = new ArrayList();
// 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 queue = new PriorityQueue();
// 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 set = new HashSet();
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 map = new HashMap();
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 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 map = new TreeMap();
map.put("a", "Aha");
map.put("b", "Boo");
map.put("c", "Coon");
map.put("e", "Emu");
map.put("f", "Fox");
SortedMap 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 sorted = new TreeSet();
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 ordered = new LinkedHashSet();
ordered.add("c");
ordered.add("z");
ordered.add("a");
StringBuffer sb = new StringBuffer();
for (String s : ordered) {
sb.append(s);
}
assertEquals(sb.toString(), __);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy