Class Tuple2<A,B>

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

public class Tuple2<A,B> extends Object
Represents an immutable tuple containing two values.

This class provides a simple wrapper for two values that properly implements equals(Object) and hashCode() based on content rather than identity. This is particularly useful for HashMap composite keys when you need to combine multiple values into a single key.

Features:
  • Immutable - values 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 Tuple2 instances containing arrays with the same content will be considered equal.

Use Cases:
  • Composite keys for HashMap lookups (e.g., combining two identifiers)
  • Using arrays in composite keys with proper content-based equality
  • Returning multiple values from methods (alternative to creating a custom class)
  • Grouping related values together for processing
Usage:

// Using arrays in composite keys Map<Tuple2<String,int[]>,Result> map = new HashMap<>(); map.put(Tuple2.of("key", new int[]{1,2,3}), result1); Result r = map.get(Tuple2.of("key", new int[]{1,2,3})); // Works correctly! // Simple composite key Map<Tuple2<String,Integer>,String> cache = new HashMap<>(); cache.put(Tuple2.of("user", 12345), "John Doe"); String name = cache.get(Tuple2.of("user", 12345)); // Returns "John Doe"

Thread Safety:

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

See Also:
  • Constructor Details

    • Tuple2

      public Tuple2(A a, B b)
      Constructor.
      Parameters:
      a - Object 1.
      b - Object 2.
  • Method Details

    • of

      public static <A, B> Tuple2<A,B> of(A a, B b)
      Creates a new tuple containing the specified two values.
      Example:

      Tuple2<String,Integer> tuple = Tuple2.of("name", 42); Tuple2<String[],int[]> arrayTuple = Tuple2.of( new String[]{"a", "b"}, new int[]{1, 2} );

      Type Parameters:
      A - The type of the first value.
      B - The type of the second value.
      Parameters:
      a - The first value.
      b - The second value.
      Returns:
      A new tuple containing the specified values.
    • equals

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

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

      Tuple2<String,Integer> tuple = Tuple2.of("hello", 42); String first = tuple.getA(); // Returns "hello"

      Returns:
      The first value in this tuple.
    • getB

      public B getB()
      Returns the second value in this tuple.
      Example:

      Tuple2<String,Integer> tuple = Tuple2.of("hello", 42); Integer second = tuple.getB(); // Returns 42

      Returns:
      The second value in this tuple.
    • optA

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

      Tuple2<String,Integer> tuple = Tuple2.of("hello", 42); Optional<String> first = tuple.optA(); // Returns Optional.of("hello") Tuple2<String,Integer> tuple2 = Tuple2.of(null, 42); Optional<String> first2 = tuple2.optA(); // Returns Optional.empty()

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

      public Optional<B> optB()
      Returns the second value in this tuple wrapped in an Optional.
      Example:

      Tuple2<String,Integer> tuple = Tuple2.of("hello", 42); Optional<Integer> second = tuple.optB(); // Returns Optional.of(42) Tuple2<String,Integer> tuple2 = Tuple2.of("hello", null); Optional<Integer> second2 = tuple2.optB(); // Returns Optional.empty()

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

      public int hashCode()
      Overrides:
      hashCode in class Object