001package intermediate;
002
003import com.sandwich.koan.Koan;
004
005import java.util.*;
006
007import static com.sandwich.koan.constant.KoanConstants.__;
008import static com.sandwich.util.Assert.assertEquals;
009
010
011public class AboutCollections {
012
013    @Koan
014    public void usingAnArrayList() {
015        // List = interface
016        // The generic syntax and special generic cases will be handled in
017        // AboutGenerics. We just use <String> collections here to keep it
018        // simple.
019        List<String> list = new ArrayList<String>();
020        // ArrayList: simple List implementation
021        list.add("Chicken");
022        list.add("Dog");
023        list.add("Chicken");
024        assertEquals(list.get(0), __);
025        assertEquals(list.get(1), __);
026        assertEquals(list.get(2), __);
027    }
028
029    @Koan
030    public void usingAQueue() {
031        // Queue = interface
032        Queue<String> queue = new PriorityQueue<String>();
033        // PriorityQueue: simple queue implementation
034        queue.add("Cat");
035        queue.add("Dog");
036        assertEquals(queue.peek(), __);
037        assertEquals(queue.size(), __);
038        assertEquals(queue.poll(), __);
039        assertEquals(queue.size(), __);
040        assertEquals(queue.poll(), __);
041        assertEquals(queue.isEmpty(), __);
042    }
043
044    @Koan
045    public void usingABasicSet() {
046        Set<String> set = new HashSet<String>();
047        set.add("Dog");
048        set.add("Cat");
049        set.add("Dog");
050        assertEquals(set.size(), __);
051        assertEquals(set.contains("Dog"), __);
052        assertEquals(set.contains("Cat"), __);
053        assertEquals(set.contains("Chicken"), __);
054    }
055
056    @Koan
057    public void usingABasicMap() {
058        Map<String, String> map = new HashMap<String, String>();
059        map.put("first key", "first value");
060        map.put("second key", "second value");
061        map.put("first key", "other value");
062        assertEquals(map.size(), __);
063        assertEquals(map.containsKey("first key"), __);
064        assertEquals(map.containsKey("second key"), __);
065        assertEquals(map.containsValue("first value"), __);
066        assertEquals(map.get("first key"), __);
067    }
068
069    @Koan
070    public void usingBackedArrayList() {
071        String[] array = {"a", "b", "c"};
072        List<String> list = Arrays.asList(array);
073        list.set(0, "x");
074        assertEquals(array[0], __);
075        array[0] = "a";
076        assertEquals(list.get(0), __);
077        // Just think of it as quantum state teleportation...
078    }
079
080    @Koan
081    public void usingBackedSubMap() {
082        TreeMap<String, String> map = new TreeMap<String, String>();
083        map.put("a", "Aha");
084        map.put("b", "Boo");
085        map.put("c", "Coon");
086        map.put("e", "Emu");
087        map.put("f", "Fox");
088        SortedMap<String, String> backedMap = map.subMap("c", "f");
089        assertEquals(backedMap.size(), __);
090        assertEquals(map.size(), __);
091        backedMap.put("d", "Dog");
092        assertEquals(backedMap.size(), __);
093        assertEquals(map.size(), __);
094        assertEquals(map.containsKey("d"), __);
095        // Again: backed maps are just like those little quantum states
096        // that are connected forever...
097    }
098
099    @Koan
100    public void differenceBetweenOrderedAndSorted() {
101        TreeSet<String> sorted = new TreeSet<String>();
102        sorted.add("c");
103        sorted.add("z");
104        sorted.add("a");
105        assertEquals(sorted.first(), __);
106        assertEquals(sorted.last(), __);
107        // Look at the different constructors for a TreeSet (or TreeMap)
108        // Ponder how you might influence the sort order. Hold that thought
109        // until you approach AboutComparison
110
111        LinkedHashSet<String> ordered = new LinkedHashSet<String>();
112        ordered.add("c");
113        ordered.add("z");
114        ordered.add("a");
115        StringBuffer sb = new StringBuffer();
116        for (String s : ordered) {
117            sb.append(s);
118        }
119        assertEquals(sb.toString(), __);
120    }
121}