<< Chapter < Page Chapter >> Page >
The module describes comparator objects for determining a binary order and builds a generalized priority queue with it.

Order relation

When comparing two objects, there are two ways to look at the comparison behavior, which creates a notion of ordering between the two objects:

  1. Each object knows how to compare itself -- Comparison is considered intrinsic to the object and is part of its behavior.
  2. Comparison is done by an external object -- A third object is introduced that contains the ability to compare two objects.

These two different outlooks on binary object ordering will be explored below in terms of the two Java interfaces that are used to model them.

java.lang.Comparable

There are many computing tasks that require performing some sort of comparison between data objects. A few data types are endowed with a "natural" ordering of their values. The integers have a natural ordering "less or equal to", labeled "<=", defined as follows.

n<= m iff m = n + k, for some non-negative integer k
( Note : a rigorous mathematical definition of the set of non-negative integers is beyond the scope of this lecture).

The above natural order of the integers is a concrete instance of an abstract concept called an order relation. An order relation on a set S is a boolean function R on S x S that is

  • reflexive: R(x, x) is true for all x in S,
  • anti-symmetric: R(x,y) and R(y,x) implies x = y, for all x, y in S, and
  • transitive: R(x, y) and R(y, z) implies R(x, z), for all x, y, z in S.

To model order relations that are naturally endowed in certain types of data objects, Java provides an interface called Comparable , which has exactly one method called

int compareTo(Object rhs) , defined abstractly as

  • x.compareTo(y)<0 means x is "less than" y ,
  • x.compareTo(y) == 0 means x is "equal to" y , and
  • x.compareTo(y)>0 means y is "less than" x .

For example, the Integer class implements the Comparable interface as follows. If x and y are Integer objects then,

  • x.compareTo(y)<0 means x<y ,
  • x.compareTo(y) == 0 means x == y , and
  • x.compareTo(y)>0 means y<x .

Common data types that have a natural ordering among their values, such as Double , String , Character , all implement Comparable .

    Advantages and disadvantages

  • Advantages
    • Objects carry their ability to be compared with them and thus can be involved in comparison operations anywhere.
    • The comparison operation is invariant and thus consistent in all situations.
  • Disadvantages
    • Comparison operation cannot be changed to match different situations.

java.util.Comparator

Most of the time, the ordering among the data objects is an extrinsic operation imposed on the object by the user of the objects. For example, the Pizza objects in homework 2 have no concepts of comparing among themselves, however, the user can impose an ordering on them by comparing their price/area ratios or their profits. To model extrinsic ordering relation, Java provides an interface in the java.util package called Comparator , which has exactly two methods:

  • int compare(Object x, Object y) , to model the ordering
    • compare(x, y)<0 means x is "less than" y ,
    • compare(x, y) == 0 means x is "equal to" y , and
    • compare(x, y)>0 means yis "less than" x , and
  • boolean equals(Object x) , to model equality of Comparators . Unlike the equals method of most objects, equality of Comparator s also requires that their comparison behavior be identical.

Questions & Answers

what is mutation
Janga Reply
what is a cell
Sifune Reply
how is urine form
Sifune
what is antagonism?
mahase Reply
classification of plants, gymnosperm features.
Linsy Reply
what is the features of gymnosperm
Linsy
how many types of solid did we have
Samuel Reply
what is an ionic bond
Samuel
What is Atoms
Daprince Reply
what is fallopian tube
Merolyn
what is bladder
Merolyn
what's bulbourethral gland
Eduek Reply
urine is formed in the nephron of the renal medulla in the kidney. It starts from filtration, then selective reabsorption and finally secretion
onuoha Reply
State the evolution relation and relevance between endoplasmic reticulum and cytoskeleton as it relates to cell.
Jeremiah
what is heart
Konadu Reply
how is urine formed in human
Konadu
how is urine formed in human
Rahma
what is the diference between a cavity and a canal
Pelagie Reply
what is the causative agent of malaria
Diamond
malaria is caused by an insect called mosquito.
Naomi
Malaria is cause by female anopheles mosquito
Isaac
Malaria is caused by plasmodium Female anopheles mosquitoe is d carrier
Olalekan
a canal is more needed in a root but a cavity is a bad effect
Commander
what are pathogens
Don Reply
In biology, a pathogen (Greek: πάθος pathos "suffering", "passion" and -γενής -genēs "producer of") in the oldest and broadest sense, is anything that can produce disease. A pathogen may also be referred to as an infectious agent, or simply a germ. The term pathogen came into use in the 1880s.[1][2
Zainab
A virus
Commander
Definition of respiration
Muhsin Reply
respiration is the process in which we breath in oxygen and breath out carbon dioxide
Achor
how are lungs work
Commander
where does digestion begins
Achiri Reply
in the mouth
EZEKIEL
what are the functions of follicle stimulating harmones?
Rashima Reply
stimulates the follicle to release the mature ovum into the oviduct
Davonte
what are the functions of Endocrine and pituitary gland
Chinaza
endocrine secrete hormone and regulate body process
Achor
while pituitary gland is an example of endocrine system and it's found in the Brain
Achor
what's biology?
Egbodo Reply
Biology is the study of living organisms, divided into many specialized field that cover their morphology, physiology,anatomy, behaviour,origin and distribution.
Lisah
biology is the study of life.
Alfreda
Biology is the study of how living organisms live and survive in a specific environment
Sifune
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Principles of object-oriented programming. OpenStax CNX. May 10, 2013 Download for free at http://legacy.cnx.org/content/col10213/1.37
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Principles of object-oriented programming' conversation and receive update notifications?

Ask