// Helene Martin, CSE 143 // Demonstrates behavior that requires classes to implement the Comparable interface. import java.util.*; public class CollectionsOrdering { public static void main(String[] args) { testStringOrdering(); testPointOrdering(); testHtmlTagOrdering(); } // Builds a List of Strings and calls several methods that require Strings // to have a natural ordering. public static void testStringOrdering() { String[] tempNames = { "Vinod", "Colin", "Daniel", "Martin", "Dane" }; List names = Arrays.asList(tempNames); System.out.println(names); Collections.sort(names); System.out.println(names); System.out.println(Collections.binarySearch(names, "Daniel")); SortedSet taSet = new TreeSet(); taSet.addAll(names); } // Builds a List of Points and calls several methods that require Points // to have a natural ordering. public static void testPointOrdering() { Point[] tempPoints = { new Point(8, 12), new Point(3, 17), new Point(3, 16), new Point(7, 5), new Point(-7, -1) }; List points = Arrays.asList(tempPoints); System.out.println(points); Collections.sort(points); System.out.println(points); System.out.println(Collections.binarySearch(points, new Point(8, 6))); SortedSet pointSet = new TreeSet(); pointSet.addAll(points); } public static void testHtmlTagOrdering() { List tags = new ArrayList(); tags.add(new HtmlTag("html", true)); // tags.add(new HtmlTag("body", true)); // tags.add(new HtmlTag("b", true)); // tags.add(new HtmlTag("b", false)); // tags.add(new HtmlTag("i", true)); // tags.add(new HtmlTag("b", true)); // tags.add(new HtmlTag("b", false)); // tags.add(new HtmlTag("br")); //
tags.add(new HtmlTag("i", false)); //
tags.add(new HtmlTag("body", false)); // tags.add(new HtmlTag("html", false)); // System.out.println(tags); Collections.sort(tags); System.out.println(tags); System.out.println(Collections.binarySearch(tags, new HtmlTag("b", false))); SortedSet tagSet = new TreeSet(); tagSet.addAll(tags); } }