Class Tuple1<A>
- Type Parameters:
A- The type of the value in this tuple.
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:
Thread Safety:
This class is immutable and therefore thread-safe. Multiple threads can safely access a Tuple1 instance concurrently without synchronization.
See Also:
Tuple2- Two-value tupleTuple3- Three-value tupleTuple4- Four-value tupleTuple5- Five-value tuple- juneau-commons Basics
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Tuple1
Constructor.- Parameters:
a- Object value.
-
-
Method Details
-
of
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
-
getA
Returns the value contained in this tuple.Example:
Tuple1<String>
tuple = Tuple1.of ("hello" ); Stringvalue =tuple .getA();// Returns "hello" - Returns:
- The value contained in this tuple.
-
optA
Returns the value contained in this tuple wrapped in anOptional.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
-