C3.java

import java.util.*;

/**
Test subranges of sorted collections.
*/
class C3
{
    public static void main(String argv[])
    {
        Iterator y;
        String a,b;
        SortedSet x2;

        TreeSet x = new TreeSet(new Comparator() {
            public int compare(Object a,Object b) {
                /*
                Sort on length, then lexicographically.
                */
                String aa = (String)a;
                String bb = (String)b;
                if (aa.length() == bb.length())
                    return aa.compareTo(bb);
                else
                    return aa.length() - bb.length();
                }
            });

        x.add("Tufted titmouse");
        x.add("White-breasted nuthatch");
        x.add("Red-breasted nuthatch");
        x.add("Downy woodpecker");
        x.add("Hairy woodpecker");
        x.add("Red-bellied woodpecker");
        x.add("Junco");
        x.add("House finch");
        x.add("House sparrow");
        x.add("Bluejay");
        x.add("Cardinal");
        x.add("Ruby-throated hummingbird");
        x.add("Sparrow");
        x.add("Carolina wren");
        x.add("Black-capped chickadee");
        x.add("Robin");
        x.add("Cowbird");
        x.add("Catbird");
        x.add("Mockingbird");
        x.add("Grackle");
        x.add("Crow");
        x.add("Red-winged blackbird");

        System.out.println("\nThe original list:");
        y = x.iterator();
        while (y.hasNext()) {
            Object z = y.next();
            System.out.println("   " + z);
            }

        a = "Carolina wren";
        b = "Black-capped chickadee";
        System.out.println("\nThe list from '" + 
            a + "' to '" + b + "':");
        System.out.println();
        x2 = x.subSet(a,b);
        y = x2.iterator();
        while (y.hasNext()) {
            Object z = y.next();
            System.out.println("   " + z);
            }

        a = "AAAAAAAA";
        b = "GGGGGGGGGGGGG";
        System.out.println("\nThe list from '" + 
            a + "' to '" + b + "':");
        System.out.println();
        x2 = x.subSet(a,b);
        y = x2.iterator();
        while (y.hasNext()) {
            Object z = y.next();
            System.out.println("   " + z);
            }
    }
}