Class HashKey

java.lang.Object
org.apache.juneau.commons.collections.HashKey

public class HashKey extends Object
Represents a composite key composed of multiple values, suitable for use as a key in hash-based collections.

This class provides an immutable, thread-safe way to create composite keys from multiple values. It's commonly used for caching scenarios where you need to uniquely identify objects based on multiple configuration parameters or attributes.

Usage Pattern:

// Create a composite key from multiple values HashKey key = HashKey.of( "config1", "config2", true, 42 ); // Use as a key in a cache or map Map<HashKey, MyObject> cache = new HashMap<>(); cache.put(key, myObject); // Retrieve using an equivalent key HashKey lookupKey = HashKey.of("config1", "config2", true, 42); MyObject cached = cache.get(lookupKey); // Returns myObject

Important Notes:
  • All relevant values must be included - When using HashKey for caching, ensure all values that affect the object's identity are included. Missing values can cause different objects to incorrectly share the same cache entry.
  • Order matters - The order of arguments in of(Object...) is significant. Two keys with the same values in different orders will not be equal.
  • Immutable - Once created, a HashKey cannot be modified. This ensures keys remain stable when used in hash-based collections.
  • Thread-safe - This class is thread-safe and can be safely used as keys in concurrent collections and caches.
  • Null values are supported - null values can be included in the key and are handled correctly in equality comparisons.
Example:

// Create keys for caching based on configuration HashKey key1 = HashKey.of("json", true, false); HashKey key2 = HashKey.of("json", true, false); HashKey key3 = HashKey.of("json", true, true); // key1 and key2 are equal (same values in same order) key1.equals(key2); // true key1.hashCode() == key2.hashCode(); // true // key1 and key3 are different (different values) key1.equals(key3); // false // Use in a cache Cache<HashKey, Processor> processorCache = Cache.create(); processorCache.put(key1, new JsonProcessor()); Processor p = processorCache.get(key2); // Returns cached instance

  • Method Details

    • of

      public static HashKey of(Object... array)
      Creates a new hash key from the specified values.

      The order of arguments is significant - two calls with the same values in the same order will produce equal HashKey instances, while different orders or values will produce different keys.

      Example:

      HashKey key1 = HashKey.of("a", "b", true); HashKey key2 = HashKey.of("a", "b", true); key1.equals(key2); // true HashKey key3 = HashKey.of("a", "b", false); key1.equals(key3); // false // Order matters HashKey key4 = HashKey.of("b", "a", true); key1.equals(key4); // false (different order)

      Parameters:
      array - The values that compose this composite key. All values that affect the key's identity should be included.
      Can be empty (produces a key representing no values).
      Can contain null values (handled correctly in equality comparisons).
      Returns:
      A new immutable hash key instance.
    • equals

      public boolean equals(Object o)
      Compares this hash key with another object for equality.

      Two HashKey instances are considered equal if they contain the same values in the same order. The comparison uses deep equality checking for array elements via Utils.eq(Object, Object).

      This method does not perform null or type checking - it assumes the caller has verified the object is a non-null HashKey instance. Passing null or a non-HashKey object will result in a ClassCastException or NullPointerException.

      Overrides:
      equals in class Object
      Parameters:
      o - The object to compare with (must be a non-null HashKey instance).
      Returns:
      true if the objects are equal, false otherwise.
    • hashCode

      public int hashCode()
      Returns the hash code for this hash key.

      The hash code is computed from all values in the key using Arrays.deepHashCode(Object[]). This ensures that equal keys have equal hash codes, making HashKey suitable for use as keys in hash-based collections like HashMap and HashSet. Arrays with the same contents (but different references) will produce the same hash code.

      Overrides:
      hashCode in class Object
      Returns:
      The hash code value for this object.
    • properties

    • toString

      public String toString()
      Overrides:
      toString in class Object