CS 2 and Java’s Comparator Interface
Download
Report
Transcript CS 2 and Java’s Comparator Interface
CS 2 and Java’s
Comparator Interface
McCloskey, Beidler, & Bi
Computing Sciences
University of Scranton
CS 2 and Java’s
Comparator Interface
• Using Interfaces
• Comparable versus Comparator
• Why aren’t you using the Comparator
Interface?
• How to apply the Comparator Interface
• Observations
Using Interfaces
• Java Interfaces
– Specifications: Let’s you say what is expected
to be accomplished without committing to
how it will be carried out
– Standards: Let’s you describe a standard that
may be applied over a collection of classes
• A great software engineering tool
Comparable versus Comparator
• Comparable Interface
– Standard that is applied when a class has a
natural order.
– Implemented within the class with a
compareTo method.
– The compareTo method should apply a total
ordering on the class
Comparable versus Comparator
• Comparator Interface
– Standard that is applied to describe a
problem dependent ordering of a class.
– Implemented outside the class with a class
that contains a compare method that maps a
total ordering onto the objects in the target
class.
– Frequently the compare method makes use
of the compareTo methods of a class’s
instance variables
Why aren’t you using the
Comparator Interface?
• Didn’t really know about it
• The Comparable interface is good enough.
I can do everything with it. I don’t need the
Comparator interface.
• Too be honest, I couldn’t quite figure out
how to implement it.
Why aren’t you using the
Comparator Interface?
• If you are overusing the Comparable
interface and not using the Comparator
interface (like most text books) you are
doing a disservice to your students.
• Why?
Why aren’t you using the
Comparator Interface?
• Every problem dependent ordering should
be carried out with a Comparator.
Why aren’t you using the
Comparator Interface?
• Every class that expects to order objects
from another class should be constructed
to accept the ordering from a comparator.
– Array Ordering, logical ordering
– Ordered List, skip list
– BST, AVL, …
• Writing an ordering class to use
comparators makes it industrial strength; it
can order anything.
How to apply the Comparator
Interface
• Constructing a class that implements the
comparator interface
public class MyComparator implements Comparator{
public int compare(Object L, Object R){
// returns a negative int if L<R
//
0 if L==P
//
a positive int if L>R
}
}
How to apply the Comparator
Interface
• Illustration: Assume that objects in a class
Able have an instance variable accessed
by getData() and it is a Comparable
object.
public class MyCtor implements Comparator{
public int compare(ObjectL, Object R){
return ((Able)L).getData().compareTo(((Able)R).getData());
}
}
How to apply the Comparator
Interface
• Comparator may include a constructor
public class MyCtor implements Comparator{
MyCtor (){ . . . }
public int compare(ObjectL, Object R){ . . . }
}
How to apply the Comparator
Interface
Comparator
Class
Application
How to apply the Comparator
Interface
• Student Class: Possible orderings
– Alpha by last name
– By class (no. of credits)
– By ZIP
– By major
– By various combinations of instance variables
How to apply the Comparator
Interface
Comparator1
Application1
Class
Comparator2
Application2
Applicationn
Comparatorn
How to apply the Comparator
Interface
Comparator1
Class
Comparator2
Application
Comparatorn
How to apply the Comparator
Interface
Comparator c = new SomeComparator();
…
BinSearchTree T = new BinSearchTree(c);
…
Using a comparator to implement an
ordering class, like a BST, expands
reusability (genericity) of the ordering
class.
Observations
• See examples in the paper
– Student class
• Basic comparators – simple orderings
• Compound comparators
– By class, by GPA, by …
– By any logical combination of the above
• Logical (indirect) ordering
Conclusions
• Are you teaching Java.
• Or are you teaching how to develop
software in Java using, and building upon,
the API
• Applying the Comparator interface for all
problem dependent orderings enhances
software reuse.
Conclusions
• Our resources
http://www.cs.uofs.edu/~beidler/cmps240/text/
Username: cmps240
Password: bebimc