CS102 · Interfaces · Annotated SlidesComparable, Comparator and Interfaces
Original
Overview

Comparable, Comparator and Interfaces

The Comparable interface and compareTo (negative / zero / positive), sorting objects with Arrays.sort, the Fruit and Person examples, and the Comparator interface (compare, equals) for sorting by different attributes such as grade or name.

How to read this page

Every original slide is shown unchanged. Yellow marks on a slide point to a numbered note: hover or tap a mark to jump to its note, and hover a note to see its marks. Questions tagged Past exam appeared on a real paper; Prediction questions are likely angles, not confirmed. Pick an answer to each question: a wrong pick tells you why it is wrong, so you can try again.

All 20 slides
  1. 1 Slide 1
  2. 2 Slide 2
  3. 3 Objective
  4. 4 — Java has many predefined interfaces that are used by
  5. 5 The Comparable Interface
  6. 6 — The compareTo method should return
  7. 7 — Class Person has name and age as instance variables — Let class person implement interface Comparable
  8. 8 Fruit Example
  9. 9 Java doesn’t know how to compare two instances of the
  10. 10 The Arrays.sort method has been written with the
  11. 11 Slide 11
  12. 12 Lexicographic ordering is same as alphabetical
  13. 13 — That is, if we have two strings str1 and str2 then
  14. 14 Fruit Code
  15. 15 The Comparator interface
  16. 16 The compare Method:
  17. 17 Example of using Comparator interface
  18. 18 We create an array of Students, three objects of type
  19. 19 Output
  20. 20 The equals Method:
Slide 1 / 20

Slide 1

Slide 1: Slide 1
Slide 2 / 20

Slide 2

Slide 2: Slide 2
Slide 3 / 20

Objective

Slide 3: Objective
Slide 4 / 20

— Java has many predefined interfaces that are used by

Slide 4: — Java has many predefined interfaces that are used by
Slide 5 / 20

The Comparable Interface

Slide 5: The Comparable Interface
Slide 6 / 20

— The compareTo method should return

Slide 6: — The compareTo method should return

Exam angle

PredictionQ1

"apple".compareTo("orange") returns:

Slide 7 / 20

— Class Person has name and age as instance variables — Let class person implement interface Comparable

Slide 7: — Class Person has name and age as instance variables — Let class person implement interface Comparable
Slide 8 / 20

Fruit Example

Slide 8: Fruit Example
Slide 9 / 20

Java doesn’t know how to compare two instances of the

Slide 9: Java doesn’t know how to compare two instances of the
Slide 10 / 20

The Arrays.sort method has been written with the

Slide 10: The Arrays.sort method has been written with the

Exam angle

PredictionQ1

Arrays.sort can sort an array of your own class even if it doesn’t implement Comparable (and no Comparator is given).

Slide 11 / 20

Slide 11

Slide 11: Slide 11
Slide 12 / 20

Lexicographic ordering is same as alphabetical

Slide 12: Lexicographic ordering is same as alphabetical
Slide 13 / 20

— That is, if we have two strings str1 and str2 then

Slide 13: — That is, if we have two strings str1 and str2 then
Slide 14 / 20

Fruit Code

Slide 14: Fruit Code
Slide 15 / 20

The Comparator interface

Slide 15: The Comparator interface

Notes

  1. Comparator (java.util) defines compare(a, b) and equals; use it for orderings other than the natural one.

Slide 16 / 20

The compare Method:

Slide 16: The compare Method:
Slide 17 / 20

Example of using Comparator interface

Slide 17: Example of using Comparator interface

Notes

  1. Use Comparator to sort by different attributes: Arrays.sort(students, new GradeComparator()).

  2. Memory trick Comparable = ONE built-in order inside the class. Comparator = MANY orders in separate classes.

Exam angle

PredictionQ1

To sort students sometimes by name and sometimes by grade, you should use:

Slide 18 / 20

We create an array of Students, three objects of type

Slide 18: We create an array of Students, three objects of type
Slide 19 / 20

Output

Slide 19: Output
Slide 20 / 20

The equals Method:

Slide 20: The equals Method: