Java Reference
In-Depth Information
Understand natural ordering vs. comparators.
Classes that implement
Comparable
are
said to have a natural ordering. If a class does not have a natural ordering, use a separate
Comparator
object to sort and search instances of the class. The
Comparator
interface
declares the compare method that returns an
int
. The return value is a negative integer,
zero, or a positive integer if the fi rst argument is less than, equal to, or greater than the sec-
ond, respectively.
Understand the difference between
==
and
equals
.
The comparison operator
==
returns
true if the two references being compared point to the same object. The
equals
method
compares two objects and uses business logic. If you override
equals
, make sure you also
override the
hashCode
method in such a way that two equal objects produce the same hash
code.
Understand the behavior and usage of the
sort
and
binarySearch
methods.
You should
be able to understand code that uses the
sort
and
binarySearch
methods of the
Collec-
tions
(for lists) and
Arrays
(for arrays) classes. The
Collections
class defi nes two static
sort
methods: one for natural-order sorting and one for comparator sorting of lists. The
Collections
class also defi nes two static
binarySearch
methods, and the list must be
sorted before it is searched. The
Arrays
class contains two overloaded
sort
methods for
each type of array, which provides for both natural-order and comparator sorting. Simi-
larly, the
Arrays
class contains
binarySearch
methods for searching the various types of
arrays, and an array must be sorted before it is searched.
Search WWH ::
Custom Search