Record Class Pair<L,R>
java.lang.Object
java.lang.Record
cloud.opencode.base.core.tuple.Pair<L,R>
- Type Parameters:
L- left value type - 左值类型R- right value type - 右值类型- Record Components:
left- left value (first element) - 左值(第一个元素)right- right value (second element) - 右值(第二个元素)
- All Implemented Interfaces:
Serializable
Pair - Immutable two-element tuple (Record implementation)
二元组 - 不可变的两元素元组(Record 实现)
Immutable key-value pair container for returning two related values.
不可变的键值对容器,可用于返回两个相关值。
Features | 主要功能:
- Immutable storage for two values - 不可变的双值存储
- Multiple aliases (left/right, first/second, key/value) - 多种别名访问
- Element mapping and transformation - 元素映射和转换
- Map.Entry interoperability - 与 Map.Entry 互操作
Usage Examples | 使用示例:
Pair<String, Integer> pair = Pair.of("name", 25);
String key = pair.left();
Pair<String, String> mapped = pair.mapRight(String::valueOf);
Map.Entry<String, Integer> entry = pair.toEntry();
Security | 安全性:
- Thread-safe: Yes (immutable record) - 线程安全: 是 (不可变 Record)
- Null-safe: Allows null values - 空值安全: 允许 null 值
- Since:
- JDK 25, opencode-base-core V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanChecks if all values are non-null 检查是否都非 null<T> Tapply(BiFunction<? super L, ? super R, ? extends T> function) Applies a bi-function 应用二元函数static <L,R> Pair <L, R> empty()Creates an empty Pair 创建空二元组final booleanIndicates whether some other object is "equal to" this one.first()Gets the first element (alias for left) 获取第一个元素(左值的别名)static <K,V> Pair <K, V> Creates a Pair from Map.Entry 从 Map.Entry 创建二元组final inthashCode()Returns a hash code value for this object.booleanhasNull()Checks if it contains a null value 检查是否包含 null 值key()Gets the key (alias for left, for Map scenarios) 获取键(左值的别名,用于 Map 场景)left()Returns the value of theleftrecord component.<T,U> Pair <T, U> Maps both left and right values 同时映射左右值Maps the left value 映射左值Maps the right value 映射右值static <L,R> Pair <L, R> of(L left, R right) Creates a Pair 创建二元组right()Returns the value of therightrecord component.second()Gets the second element (alias for right) 获取第二个元素(右值的别名)swap()Swaps left and right values 交换左右值Object[]toArray()Converts to an array 转换为数组toEntry()Converts to Map.Entry 转换为 Map.EntrytoString()Returns a string representation of this record class.value()Gets the value (alias for right, for Map scenarios) 获取值(右值的别名,用于 Map 场景)
-
Constructor Details
-
Pair
-
-
Method Details
-
of
Creates a Pair 创建二元组- Type Parameters:
L- left value type | 左值类型R- right value type | 右值类型- Parameters:
left- left value | 左值right- right value | 右值- Returns:
- the Pair | 二元组
-
fromEntry
-
empty
Creates an empty Pair 创建空二元组- Type Parameters:
L- left value type | 左值类型R- right value type | 右值类型- Returns:
- an empty Pair | 空二元组
-
first
-
second
Gets the second element (alias for right) 获取第二个元素(右值的别名)- Returns:
- right value | 右值
-
key
Gets the key (alias for left, for Map scenarios) 获取键(左值的别名,用于 Map 场景)- Returns:
- left value | 左值
-
value
Gets the value (alias for right, for Map scenarios) 获取值(右值的别名,用于 Map 场景)- Returns:
- right value | 右值
-
swap
-
mapLeft
-
mapRight
-
map
public <T,U> Pair<T,U> map(Function<? super L, ? extends T> leftMapper, Function<? super R, ? extends U> rightMapper) Maps both left and right values 同时映射左右值- Type Parameters:
T- new left type | 新左值类型U- new right type | 新右值类型- Parameters:
leftMapper- left mapper function | 左值映射函数rightMapper- right mapper function | 右值映射函数- Returns:
- a new Pair | 新二元组
-
apply
Applies a bi-function 应用二元函数- Type Parameters:
T- result type | 结果类型- Parameters:
function- the bi-function | 二元函数- Returns:
- the function result | 函数结果
-
hasNull
public boolean hasNull()Checks if it contains a null value 检查是否包含 null 值- Returns:
- true if any value is null | 如果任一值为 null 返回 true
-
allNonNull
public boolean allNonNull()Checks if all values are non-null 检查是否都非 null- Returns:
- true if all values are non-null | 如果都非 null 返回 true
-
toEntry
-
toArray
Converts to an array 转换为数组- Returns:
- an array containing two elements | 包含两个元素的数组
-
toString
-
hashCode
-
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared withObjects::equals(Object,Object). -
left
-
right
-