Class ObjectComparator

java.lang.Object
org.dellroad.stuff.java.ObjectComparator
All Implemented Interfaces:
Comparator<Object>

public class ObjectComparator extends Object implements Comparator<Object>
A Comparator that creates a stable, total ordering of all Java objects.

Instances are compared first using identity hash codes, and then using an internally generated unique identifier; identifiers are indexed using weak keys to avoid memory leaks. Any two distict Java objects will always compare as non-equal.

Note: this ordering is only consistent with equals (see Comparable) for classes whose equals() method is implemented in terms of object equality (e.g., not overridden at all).

Note: while each instance creates a stable sort ordering, distinct instances of this class may sort objects differently.

Null values are supported and always sort last. Instances of this class are thread safe.

This class requires Google Guava.

  • Constructor Details

    • ObjectComparator

      public ObjectComparator()
      Default constructor.
    • ObjectComparator

      public ObjectComparator(int concurrencyLevel)
      Constructor.
      Parameters:
      concurrencyLevel - guides the allowed concurrency among update operations; used as a hint for internal sizing
      Throws:
      IllegalArgumentException - if concurrencyLevel is zero or less
      See Also:
      • CacheBuilder.concurrencyLevel
    • ObjectComparator

      protected ObjectComparator(CacheBuilder<Object,Object> cacheBuilder)
      Internal constructor.
      Parameters:
      cacheBuilder - object unique ID cache builder
      Throws:
      NullPointerException - if cacheBuilder is null
  • Method Details