<< 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

Preparation and Applications of Nanomaterial for Drug Delivery
Hafiz Reply
Application of nanotechnology in medicine
what is variations in raman spectra for nanomaterials
Jyoti Reply
I only see partial conversation and what's the question here!
Crow Reply
what about nanotechnology for water purification
RAW Reply
please someone correct me if I'm wrong but I think one can use nanoparticles, specially silver nanoparticles for water treatment.
Damian
yes that's correct
Professor
I think
Professor
what is the stm
Brian Reply
is there industrial application of fullrenes. What is the method to prepare fullrene on large scale.?
Rafiq
industrial application...? mmm I think on the medical side as drug carrier, but you should go deeper on your research, I may be wrong
Damian
How we are making nano material?
LITNING Reply
what is a peer
LITNING Reply
What is meant by 'nano scale'?
LITNING Reply
What is STMs full form?
LITNING
scanning tunneling microscope
Sahil
how nano science is used for hydrophobicity
Santosh
Do u think that Graphene and Fullrene fiber can be used to make Air Plane body structure the lightest and strongest. Rafiq
Rafiq
what is differents between GO and RGO?
Mahi
what is simplest way to understand the applications of nano robots used to detect the cancer affected cell of human body.? How this robot is carried to required site of body cell.? what will be the carrier material and how can be detected that correct delivery of drug is done Rafiq
Rafiq
if virus is killing to make ARTIFICIAL DNA OF GRAPHENE FOR KILLED THE VIRUS .THIS IS OUR ASSUMPTION
Anam
analytical skills graphene is prepared to kill any type viruses .
Anam
Any one who tell me about Preparation and application of Nanomaterial for drug Delivery
Hafiz
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
Bob
The nanotechnology is as new science, to scale nanometric
brayan
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Damian
Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
Renato
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
?
Kyle
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
Adin
why?
Adin
what school?
Kyle
biomolecules are e building blocks of every organics and inorganic materials.
Joe
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
research.net
kanaga
sciencedirect big data base
Ernesto
Introduction about quantum dots in nanotechnology
Praveena Reply
hi
Loga
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!





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