org.apache.commons.collections4.comparators

## Class NullComparator<E>

- java.lang.Object
- - org.apache.commons.collections4.comparators.NullComparator<E>

- Type Parameters:
    `E` - the type of objects compared by this comparator
  All Implemented Interfaces:
    Serializable, Comparator<E>

      public class NullComparator<E>
      extends Object
      implements Comparator<E>, Serializable

  A Comparator that will compare nulls to be either lower or higher than other objects.
  Since:
    2.0
  See Also:
    Serialized Form

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `NullComparator()` Construct an instance that sorts `null` higher than any non-`null` object it is compared with.
    `NullComparator(boolean nullsAreHigh)` Construct an instance that sorts `null` higher or lower than any non-`null` object it is compared with.
    `NullComparator(Comparator<? super E> nonNullComparator)` Construct an instance that sorts `null` higher than any non-`null` object it is compared with.
    `NullComparator(Comparator<? super E> nonNullComparator, boolean nullsAreHigh)` Construct an instance that sorts `null` higher or lower than any non-`null` object it is compared with.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `int` | `compare(E o1, E o2)` Perform a comparison between two objects.
    `boolean` | `equals(Object obj)` Determines whether the specified object represents a comparator that is equal to this comparator.
    `int` | `hashCode()` Implement a hash code for this comparator that is consistent with `equals(Object)`.

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait`

    - ### Methods inherited from interface java.util.Comparator

      `comparing, comparing, comparingDouble, comparingInt, comparingLong, naturalOrder, nullsFirst, nullsLast, reversed, reverseOrder, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong`

- - ### Constructor Detail

    - #### NullComparator

          public NullComparator()

      Construct an instance that sorts `null` higher than any non-`null` object it is compared with. When comparing two non-`null` objects, the `ComparableComparator` is used.

    - #### NullComparator

          public NullComparator(Comparator<? super E> nonNullComparator)

      Construct an instance that sorts `null` higher than any non-`null` object it is compared with. When comparing two non-`null` objects, the specified `Comparator` is used.
      Parameters:
        `nonNullComparator` - the comparator to use when comparing two non-`null` objects. This argument cannot be `null`
      Throws:
        `NullPointerException` - if `nonNullComparator` is `null`

    - #### NullComparator

          public NullComparator(boolean nullsAreHigh)

      Construct an instance that sorts `null` higher or lower than any non-`null` object it is compared with. When comparing two non-`null` objects, the `ComparableComparator` is used.
      Parameters:
        `nullsAreHigh` - a `true` value indicates that `null` should be compared as higher than a non-`null` object. A `false` value indicates that `null` should be compared as lower than a non-`null` object.

    - #### NullComparator

          public NullComparator(Comparator<? super E> nonNullComparator,
                                boolean nullsAreHigh)

      Construct an instance that sorts `null` higher or lower than any non-`null` object it is compared with. When comparing two non-`null` objects, the specified `Comparator` is used.
      Parameters:
        `nonNullComparator` - the comparator to use when comparing two non-`null` objects. This argument cannot be `null`
        `nullsAreHigh` - a `true` value indicates that `null` should be compared as higher than a non-`null` object. A `false` value indicates that `null` should be compared as lower than a non-`null` object.
      Throws:
        `NullPointerException` - if `nonNullComparator` is `null`

  - ### Method Detail

    - #### compare

          public int compare(E o1,
                             E o2)

      Perform a comparison between two objects. If both objects are `null`, a `0` value is returned. If one object is `null` and the other is not, the result is determined on whether the Comparator was constructed to have nulls as higher or lower than other objects. If neither object is `null`, an underlying comparator specified in the constructor (or the default) is used to compare the non-`null` objects.
      Specified by:
        `compare` in interface `Comparator<E>`
      Parameters:
        `o1` - the first object to compare
        `o2` - the object to compare it to.
      Returns:
        `-1` if `o1` is "lower" than (less than, before, etc.) `o2`; `1` if `o1` is "higher" than (greater than, after, etc.) `o2`; or `0` if `o1` and `o2` are equal.

    - #### hashCode

          public int hashCode()

      Implement a hash code for this comparator that is consistent with `equals(Object)`.
      Overrides:
        `hashCode` in class `Object`
      Returns:
        a hash code for this comparator.

    - #### equals

          public boolean equals(Object obj)

      Determines whether the specified object represents a comparator that is equal to this comparator.
      Specified by:
        `equals` in interface `Comparator<E>`
      Overrides:
        `equals` in class `Object`
      Parameters:
        `obj` - the object to compare this comparator with.
      Returns:
        `true` if the specified object is a NullComparator with equivalent `null` comparison behavior (i.e. `null` high or low) and with equivalent underlying non-`null` object comparators.

Copyright © 2001–2018 The Apache Software Foundation. All rights reserved.