Java The Comparable Interface Comparable Interface A class
Java The Comparable Interface
Comparable Interface • A class implements the Comparable interface when its objects are expected to be arranged into a particular order. • For example, the class String implements Comparable, because strings are often compared with each other and arranged into alphabetical order. • Numeric classes (such as Integer or Double) implement Comparable since number are often sorted into numeric order. • Chapter Topics: QUESTION 1: • The Comparable Interface Arrange these strings into order: • compare. To() method "orange", "apple", "plum". • natural order • collating sequence • This chapter describes the Java 5. 0 version of Comparable, which uses generics. Earlier versions of Java are somewhat different.
Answer: "apple", "orange", "plum". compare. To() could be used to make the same arrangement. • • • Comparable Interface In general, an interface consists of constants and method declarations. A class that implements an interface must implement each of the methods listed in the interface. The Comparable interface consists of just one method: – int compare. To( Class. Name obj ) // Compare this object with obj. • • • // Return a negative integer, zero, or a positive integer, // when this object is less than, equal, or greater than obj. In the above, " Class. Name " stands for the type of the objects. For example, if the objects are Strings, then " Class. Name" is String. If some objects are instances of a class that implements Comparable, then each object is less than, equal, or greater than any object of that class. compare. To() returns an integer to show which of these three relations hold. Relation object. A. compare. To( object. B ) object. A Less Than object. B Negative Integer object. A Equals object. B Zero object. A Greater Than object. B Positive Integer
For example, Only the sign of the returned integer matters if the return value is not zero. The magnitude of a returned integer does not signify anything. QUESTION 2: Examine the following declarations. They use the wrapper class Integer. "apple". compare. To("orange") Negative Integer An Integer object holds an integer as its data, plus provides several useful methods (such as compare. To) for. Negative working with integers "apple". compare. To("plum") Integer minus. Ten = new Integer( -10 ); Integer minus. Five = new Integer( -5 ); "apple". compare. To("apple") Zero Integer five = new Integer( 5 ); Integer ten = new Integer( 10 ); Zero "orange". compare. To("orange") Integer fifteen = new Integer( 15 ); "orange". compare. To("apple") Positive Integer What is the result of each of the following? Negative five. compare. To( ten ) ________ Positive ten. compare. To( five ) ________ Zero five. compare. To( five ) ________ Negative ten. compare. To( fifteen ) ________ Negative minus. Five. compare. To( ten ) ________ Positive minus. Five. compare. To( minus. Ten ) ________
Natural Order • Objects that implement compare. To() can be arranged into a natural order. • This order can be visualized as an arrangement of objects from left to right, like on a number line. • If an object A is left of another object B in this ordering, then QUESTION 3: What is "grape". compare. To( "banana" ); • object. A. compare. To(object. B) • is negative. For example, X. compare. To("orange") is negative for all the fruit X left of "orange".
Answer: positive • • • Rules for compare. To() With all objects, compare. To() works the way number comparisons work in ordinary arithmetic. Here a few rules. Most of these are fairly clear if you think about numbers. Say that A, B, and C are Integers. If A. compare. To(B) > 0 then B. compare. To(A) < 0. If A. compare. To(B) > 0 and B. compare. To(C) > 0 then A. compare. To(C) > 0. QUESTION 4: If A. compare. To(B) == 0 then A. compare. To(Z) and B. compare. To(Z) Say that X. compare. To(Y)==0. should give the same result, no matter what Z is. Is it then true that X. equals(Y)? The classes that come with Java follow these rules. If you write a class that implements Comparable, you need to follow these rules. This is not hard to do because most sensible compare. To() methods will do this naturally.
Strings!! • All the Rules • Here all the rules for comparing strings, in one annoying list: • Rule 1: If A. compare. To(B) == 0, then A and B are the same length (counting all characters, including blanks and punctuation) and each character in A is identical (including case) to the character in B at the same location. • Rule 2: Otherwise, if string A is a prefix of string B, then A. compare. To(B) < 0. If B is a prefix of string A, then A. compare. To(B) > 0. • Rule 3: Otherwise, find the first differing pair of characters in the strings A and B. Call them Achar and Bchar. Then A. compare. To(B) is negative if Achar comes before Bchar in the alphabet used by Java (and otherwise is positive). • The rules about what character comes first in the alphabet depend on what country you are in. This is one of the aspects of internationalization, which is the subject of customizing programs for use in different countries. Lets not worry about that
Sample Implementations public int compare. To( Sample. Class other ) { return get. Word(). compare. To( other. get. Word() ); }
Pop Quiz • http: //chortle. ccsu. edu/java 5/Notes/chap 53 quiz. html
To Do • Complete the Interface Assignment • Practice AP Test • Elevens Lab • On Thursday: – Practice Exam: Finish Multiple Choice – Start Free Response
- Slides: 10