T- The common type of the objects to be compared.
public class NullSafeComparator<T extends Comparable<T>> extends Object implements Comparator<T>
This comparator performs
Comparable.compareTo(Object) if both objects
are non-null, and sorts
null to be -1 (before the non-null), and
nulls as equal.
The static method
compareObjects(Object, Object) can be used if such
a comparison is needed without having to instantiate this class, for instance
because it is used within a
Comparable.compareTo(Object) or because
the two arguments are not of an agreeable subclass <
Comparable (but still can be compared).
|Constructor and Description|
|Modifier and Type||Method and Description|
Compare any two objects, null-safe.
Compare two objects for
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
If both parameters are
0. If only a
-1, if only b is
1. If none are
nullreturn might sound counter-intuitive, but it basically means that the objects could not be compared on nullity alone, and must be further compared.
a- First object to compare
b- Second object to compare
a- First object to compare. Must be instance of
b- Second object to compare. Must be
ClassCastException- if the specified object's type prevents it from being compared to this object.
Copyright © 2015–2016 The Apache Software Foundation. All rights reserved.