next next up down toc Memos schedule allInOne PDF PDF mail
all, section 9.35.

9.35.  Comparator in separate Classes

The use:

Src/9/ComparatorExTreeClass.java.minusSTART_STOP


import java.util.*;

public class ComparatorExTreeClass {
    static HpbComparator theNth = new HpbComparator();

    protected static int  soManyS;
    protected String  name;
    protected int     waitingListN;

    public ComparatorExTreeClass(String name) {
        if (name==null)
            throw new NullPointerException();
        this.name = name;
        this.waitingListN = soManyS ++;
    }

    public ComparatorExTreeClass(String name, int waitingListN) {
        this(name);
        this.waitingListN = waitingListN;
    }


    public String getName()    {
        return name;
    }

    public String toString() {
        return name + " - " + waitingListN;
    }

    public static void main(String args[]) {
        WaitingList n[] = {
            new WaitingList("Bond"),
            new WaitingList("Jack"),
            new WaitingList("Elwood"),
            new WaitingList("You", -1),
            new WaitingList("Me", -1)
        };
        TreeSet l = new TreeSet(theNth);

        for ( int i = 0; i < n.length; i ++ )   {
                System.out.println(i + " " + n[i]);
                l.add(n[i]);
        }
        System.out.println("the TreeSet: " + l);
    }
}


Source Code: Src/9/ComparatorExTreeClass.java

The comparator:

Src/9/HpbComparator.java.minusSTART_STOP



import java.util.*;

public class HpbComparator implements Comparator {

    public int compare(Object o1, Object o2) {

        if ( ( o1 instanceof WaitingList )  &&
             ( o2 instanceof WaitingList )  )   {
                WaitingList n1 = (WaitingList)o1;
                WaitingList n2 = (WaitingList)o2;
                int nameCompareV = n1.name.compareTo(n2.name);
                return ( nameCompareV == 0 ?
                        n1.waitingListN - n2.waitingListN :
                        nameCompareV);;
        } else
            return -1;
    }
}


Source Code: Src/9/HpbComparator.java


back next up down toc Memos schedule allInOne pdf PDF mail

Created by unroff, java2html & & hp-tools. © by csfac. All Rights Reserved (2010).
It is not allowed to print these pages on a CAST printer.
Last modified: 01/April/10 (17:16)