Class Tuple1<A>

java.lang.Object
org.apache.juneau.commons.function.Tuple1<A>
Type Parameters:
A - The type of the value in this tuple.

public class Tuple1<A> extends Object
Represents an immutable tuple containing a single value.

This class provides a simple wrapper for a single value that properly implements equals(Object) and hashCode() based on content rather than identity. This is particularly useful for HashMap keys when you want to use an array or need a wrapper that uses content-based equality.

Features:
  • Immutable - value cannot be changed after construction
  • Content-based equality - uses Utils.eq(Object, Object) for comparison
  • Content-based hashing - uses HashCode.of(Object...) for hash code computation
  • Array support - properly handles arrays with content-based equality and hashing
Array Support:

Unlike using arrays directly as HashMap keys (which use identity-based equality), this class properly handles arrays by using content-based equality and hashing via HashCode.of(Object...) which internally uses Arrays.hashCode(Object[]) for arrays. This means two Tuple1 instances containing arrays with the same content will be considered equal.

Use Cases:
  • Using arrays as HashMap keys with proper content-based equality
  • Wrapping values that need content-based equality for collections
  • Creating composite keys from single values
  • Passing values through APIs that require object identity preservation
Usage:

// Using arrays as keys via Tuple1 Map<Tuple1<String[]>,Integer> map = new HashMap<>(); map.put(Tuple1.of(new String[]{"a","b"}), 1); map.put(Tuple1.of(new String[]{"a","b"}), 2); // Replaces first entry System.out.println(map.size()); // Prints "1" // Using with other types Map<Tuple1<String>,Integer> stringMap = new HashMap<>(); stringMap.put(Tuple1.of("key"), 42); Integer value = stringMap.get(Tuple1.of("key")); // Returns 42

Thread Safety:

This class is immutable and therefore thread-safe. Multiple threads can safely access a Tuple1 instance concurrently without synchronization.

See Also:
  • Constructor Details

    • Tuple1

      public Tuple1(A a)
      Constructor.
      Parameters:
      a - Object value.
  • Method Details

    • of

      public static <A> Tuple1<A> of(A a)
      Creates a new tuple containing the specified value.
      Example:

      Tuple1<String> tuple = Tuple1.of("value"); Tuple1<String[]> arrayTuple = Tuple1.of(new String[]{"a", "b"});

      Type Parameters:
      A - The value type.
      Parameters:
      a - The value to wrap in the tuple.
      Returns:
      A new tuple containing the specified value.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • getA

      public A getA()
      Returns the value contained in this tuple.
      Example:

      Tuple1<String> tuple = Tuple1.of("hello"); String value = tuple.getA(); // Returns "hello"

      Returns:
      The value contained in this tuple.
    • optA

      public Optional<A> optA()
      Returns the value contained in this tuple wrapped in an Optional.
      Example:

      Tuple1<String> tuple = Tuple1.of("hello"); Optional<String> value = tuple.optA(); // Returns Optional.of("hello") Tuple1<String> tuple2 = Tuple1.of(null); Optional<String> value2 = tuple2.optA(); // Returns Optional.empty()

      Returns:
      The value wrapped in an Optional, or Optional.empty() if the value is null.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object