Java Reference

In-Depth Information

/**
See below for specs of the two methods
*/

public interface
Comparator {

boolean
equals(Object obj);

int
compare(Object o1, Object o2);

}

Function
equals
should yield
true
if (and only if)

1.
obj
is also a
Comparator
, and it imposes the same total ordering as this

Comparator
(see below).

2.
equals
is an equivalence relation, i.e. it is reflexive (
x.equals(x)
)
,

symmetric (
x.equals(y) == y.equals(x)
), and transitive: (if

x.equals(y)
and
y.equals(z)
, then
x.equals(z)
).

3. It is
total
: any two objects in the domain can be tested for equality.

Function
compare
returns
-1
,
0
, or
1
depending on whether
o1
is less than,

equal to, or greater than
o2
. Thus, this function imposes a total ordering on ele-

ments. Function
compare
should also satisfy the following:

sgn(compare(x, y)) == -sgn(compare(y, x))

In particular, if
compare (x, y)
throws an exception, so does
compare(y, x)
.

Usually,
compare(x,y) == 0) == x.equals(y)
, but it is not necessary.

12.3

Enumeration and Iterator

An
enumeration
of a set of values is simply a listing of the values. For example,

here is an enumeration of the first four natural numbers:
0
,
1
,
2
,
3
. To
enumerate

a set of values
means to provide an enumeration of it. An enumeration of a

String
would be a list of its characters. One could also provide an enumeration

of the links on an html page.

Package
java.util
contains interface
Enumeration
, which can be used to

facilitate enumerating the objects of a collection of elements. In this section, we

explore the use of this interface, as well as a newer one, interface
Iterator
.

Lesson page

12-4

Interface Enumeration

Here is interface
Enumeration
:

Activity

12-4.1

public interface
Enumeration {

/** =
"there are more objects to enumerate" */

boolean
hasMoreElements();

/** =
the next object to enumerate. If there are

no more, throw a
NoSuchElementException
*/

Object nextElement();

Get interface

Enumeration

from lesson

page 12.4.

}

Search WWH ::

Custom Search