Class 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.
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:
Thread Safety:
This class is immutable and therefore thread-safe. Multiple threads can safely access a Tuple2 instance concurrently without synchronization.
See Also:
Tuple1- Single-value tupleTuple3- Three-value tupleTuple4- Four-value tupleTuple5- Five-value tuple- juneau-commons Basics
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleangetA()Returns the first value in this tuple.getB()Returns the second value in this tuple.inthashCode()static <A,B> Tuple2<A, B> of(A a, B b) Creates a new tuple containing the specified two values.optA()Returns the first value in this tuple wrapped in anOptional.optB()Returns the second value in this tuple wrapped in anOptional.
-
Constructor Details
-
Tuple2
Constructor.- Parameters:
a- Object 1.b- Object 2.
-
-
Method Details
-
of
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
-
getA
Returns the first value in this tuple.Example:
Tuple2<String,Integer>
tuple = Tuple2.of ("hello" , 42); Stringfirst =tuple .getA();// Returns "hello" - Returns:
- The first value in this tuple.
-
getB
Returns the second value in this tuple.Example:
Tuple2<String,Integer>
tuple = Tuple2.of ("hello" , 42); Integersecond =tuple .getB();// Returns 42 - Returns:
- The second value in this tuple.
-
optA
Returns the first value in this tuple wrapped in anOptional.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
Returns the second value in this tuple wrapped in anOptional.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
-