Top Description Fields Constructors Methods
org.apache.commons.collections4.comparators

public Class ComparableComparator<E extends java.lang.Comparable<? super E>>

extends Object
implements Comparator<E extends Comparable<? super E>>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.util.Comparator
Type Parameters
<E>
the type of objects compared by this comparator
Imports
java.io.Serializable, java.util.Comparator

A Comparator that compares Comparable objects.

This Comparator is useful, for example, for enforcing the natural order in custom implementations of SortedSet and SortedMap.

Note

In the 2.0 and 2.1 releases of Commons Collections, this class would throw a ClassCastException if either of the arguments to compare were null, not Comparable, or for which compareTo gave inconsistent results. This is no longer the case. See compare for details.

Since
2.0
See Also
java.util.Collections#reverseOrder()

Field Summary

Modifier and TypeField and Description
public static final ComparableComparator<E extends Comparable<? super E>>
INSTANCE

The singleton instance.

private static final long
serialVersionUID

Serialization version.

Constructor Summary

AccessConstructor and Description
public
ComparableComparator()

Constructor whose use should be avoided.

Method Summary

Modifier and TypeMethod and Description
public static <
the element type
E extends Comparable<? super E>
>
ComparableComparator<E>

Returns:

the singleton ComparableComparator
comparableComparator
()

Gets the singleton instance of a ComparableComparator.

public int

Returns:

negative if obj1 is less, positive if greater, zero if equal
compare
(final E
the first object to compare
obj1
,
final E
the second object to compare
obj2
)

Implements java.util.Comparator.compare.

Compare the two Comparable arguments.
public boolean

Returns:

true if equal
equals
(final Object
the object to compare with
object
)

Overrides java.lang.Object.equals.

Implements java.util.Comparator.equals.

Returns true iff that Object is is a Comparator whose ordering is known to be equivalent to mine.
public int

Returns:

a hash code for this comparator.
hashCode
()

Overrides java.lang.Object.hashCode.

Implement a hash code for this comparator that is consistent with equals.
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

INSTANCEback to summary
public static final ComparableComparator<E extends Comparable<? super E>> INSTANCE

The singleton instance.

Annotations
@SuppressWarnings:rawtypes
serialVersionUIDback to summary
private static final long serialVersionUID

Serialization version.

Constructor Detail

ComparableComparatorback to summary
public ComparableComparator()

Constructor whose use should be avoided.

Please use the comparableComparator() method whenever possible.

Method Detail

comparableComparatorback to summary
public static <E extends Comparable<? super E>> ComparableComparator<E> comparableComparator()

Gets the singleton instance of a ComparableComparator.

Developers are encouraged to use the comparator returned from this method instead of constructing a new instance to reduce allocation and GC overhead when multiple comparable comparators may be used in the same VM.

Parameters
<E>
the element type
Returns:ComparableComparator<E>

the singleton ComparableComparator

Since
4.0
compareback to summary
public int compare(final E obj1, final E obj2)

Implements java.util.Comparator.compare.

Compare the two Comparable arguments. This method is equivalent to:

((Comparable)obj1).compareTo(obj2)
Parameters
obj1:E

the first object to compare

obj2:E

the second object to compare

Returns:int

negative if obj1 is less, positive if greater, zero if equal

Annotations
@Override
Exceptions
NullPointerException:
if obj1 is null, or when ((Comparable)obj1).compareTo(obj2) does
ClassCastException:
if obj1 is not a Comparable, or when ((Comparable)obj1).compareTo(obj2) does
equalsback to summary
public boolean equals(final Object object)

Overrides java.lang.Object.equals.

Implements java.util.Comparator.equals.

Returns true iff that Object is is a Comparator whose ordering is known to be equivalent to mine.

This implementation returns true iff object.getClass() equals this.getClass(). Subclasses may want to override this behavior to remain consistent with the Comparator#equals(Object) contract.

Parameters
object:Object

the object to compare with

Returns:boolean

true if equal

Annotations
@Override
Since
3.0
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Implement a hash code for this comparator that is consistent with equals.

Returns:int

a hash code for this comparator.

Annotations
@Override
Since
3.0