import java.util.Set;
import java.util.HashSet;
import java.util.TreeSet;

import java.util.Iterator;

import java.util.Map;
import java.util.HashMap;
import java.util.TreeMap;

/**
 * Example code snippet showing use of the JCF.
 * Written for CS211, Nov 2006.
 * 
 * @author Paul Chew
 */
class Snippets {
    
    /**
     * A method that checks if a given word is within a Set of words.
     */
    public static boolean findWord (String word, Set<String> set) {
        return set.contains(word);
    }
    
    /**
     * A method that removes all words longer than 5 letters from a Set.
     */
    public static void removeLong (Set<String> set) {
        for (Iterator<String> it = set.iterator(); it.hasNext();) {
            if (it.next().length() > 5) it.remove();
        }
    }
    
    /**
     * Return a new set that is the union of the two sets.
     * Both sets must be of the same type.
     */
    public static <T> Set<T> union (Set<T> a, Set<T> b) {
        Set<T> result = new HashSet<T>();
        result.addAll(a);
        result.addAll(b);
        return result;
    }
    
    /**
     * Return a new set that is the intersection of the two sets.
     * Both sets must be of the same type.
     */
    public static <T> Set<T> intersection (Set<T> a, Set<T> b) {
        Set<T> result = new HashSet<T>();
        result.addAll(a);
        result.retainAll(b);
        return result;
    }
    
    /**
     * A method that prints out a Set of words in order.
     */
    public static void printInOrder (Set<String> set) {
        System.out.println(new TreeSet<String>(set));
    }
    
    /**
     * Given a Map that maps student ID number to student name, 
     * print out a list of students sorted by ID number and 
     * another list sorted by name (assume no duplicate names)
     */
    public static void printStudents (Map<Integer,String> map) {
        System.out.println(new TreeMap<Integer,String>(map));
        TreeMap<String,Integer> other = new TreeMap<String,Integer>();
        for (Integer key: map.keySet()) other.put(map.get(key), key);
        System.out.println(other);
    }
    
    /**
     * Test code.
     */
    public static void main (String[] args) {
        String[] months = {"jan", "feb", "mar", "apr", "may", "jun", 
            "jul", "aug", "sep", "oct", "nov", "dec"};
        Set<String> set = new HashSet<String>();
        for (String m: months) set.add(m);
        String x = "nov"; 
        System.out.println("String '" + x + "' is " + (findWord(x, set)?"":"not ") + "in the set");
        x = "Nov";
        System.out.println("String '" + x + "' is " + (findWord(x, set)?"":"not ") + "in the set");
        set.add("a long String");
        set.add("another");
        System.out.println("set contains " + set.size() + " items");
        removeLong(set);
        System.out.println("After removing long items, set contains " + set.size() + " items");
        Set<String> other = new HashSet<String>();
        other.add("one"); other.add("two"); other.add("three"); other.add("nov");
        Set<String> another = union(set, other);
        System.out.println(another);
        another = intersection(set, other);
        System.out.println(another);
        printInOrder(set);
        HashMap<Integer,String> m = new HashMap<Integer,String>();
        m.put(3, "Kelly");
        m.put(1, "Paul");
        m.put(2, "Dave");
        m.put(4, "John Q. Student");
        System.out.println(m);
        printStudents(m);
    }



}
