1 |
Handout B for e03 |
CS56 F17 |
Handout B, p. 1: Useful Reference Items
Here are a few reminders of things we discussed in class, but that you might reasonably need a “reference” for if you were using them in the real world.
The interface java.util.Comparator<T>
includes the following
method signature:
int |
compare(T o1, T o2) |
Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. |
The interface java.lang.Comparable<T>
includes the following
method signature:
int |
compareTo(T o) |
Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. |
The class java.util.ArrayList<E>
includes this method:
void |
sort(Comparator<? super E> c) |
Sorts this list according to the order induced by the specified Comparator . |
The class java.util.Collections
contains the following static method:
static <T extends Comparable<? super T>> void |
sort(List<T> list) |
Sorts the specified list into ascending order, according to the natural ordering of its elements. |
The classes java.lang.String
and java.lang.Double
implement Comparable<String>
and Comparable<Double>
, each in the
way that you would expect.
Other potentially useful methods
In java.lang.Double
:
public static int |
compare(double d1, double d2) |
Compares the two specified double values. The sign of the integer value returned is the same as that of the integer that would be returned by the call: new Double(d1).compareTo(new Double(d2)) |
End of Handout