Record Class Pair<A,B>
java.lang.Object
java.lang.Record
cloud.opencode.base.collections.Pair<A,B>
- Type Parameters:
A- the type of the first element - 第一个元素的类型B- the type of the second element - 第二个元素的类型- Record Components:
first- the first element - 第一个元素second- the second element - 第二个元素
- All Implemented Interfaces:
Serializable, Map.Entry<A,B>
Pair - Immutable generic pair of two values
Pair - 不可变的泛型二元组
A simple container holding two related values. Implements Map.Entry
for seamless interoperability with Java collections.
一个持有两个相关值的简单容器。实现 Map.Entry 以便与 Java 集合无缝互操作。
Features | 主要功能:
- Immutable record-based implementation - 基于 record 的不可变实现
- Map.Entry compatibility - Map.Entry 兼容
- Functional transformations (map, swap) - 函数式变换(map、swap)
- Null-safe: allows null values - 空值安全:允许 null 值
Usage Examples | 使用示例:
// Create a pair - 创建二元组
Pair<String, Integer> pair = Pair.of("age", 25);
// Swap elements - 交换元素
Pair<Integer, String> swapped = pair.swap(); // (25, "age")
// Map values - 映射值
Pair<String, String> mapped = pair.mapSecond(String::valueOf); // ("age", "25")
// Use as Map.Entry - 作为 Map.Entry 使用
Map<String, Integer> map = Map.ofEntries(Pair.of("a", 1), Pair.of("b", 2));
- Since:
- JDK 25, opencode-base-collections V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanCompares this Pair with another object for equality, compatible with theMap.Entrycontract.first()Returns the value of thefirstrecord component.static <A,B> Pair <A, B> Creates a Pair from aMap.Entry.getKey()Returns the first element as the key.getValue()Returns the second element as the value.inthashCode()Returns a hash code consistent with theMap.Entrycontract:Objects.hashCode(getKey()) ^ Objects.hashCode(getValue()).<C> Cmap(BiFunction<? super A, ? super B, ? extends C> fn) Applies a bi-function to both elements and returns the result.Transforms the first element using the given function.Transforms the second element using the given function.static <A,B> Pair <A, B> of(A first, B second) Creates a new Pair with the given values.second()Returns the value of thesecondrecord component.Always throwsUnsupportedOperationExceptionsince Pair is immutable.swap()Returns a new Pair with the elements swapped.final StringtoString()Returns a string representation of this record class.
-
Constructor Details
-
Pair
-
-
Method Details
-
hashCode
-
equals
-
of
Creates a new Pair with the given values. 使用给定的值创建新的二元组。- Type Parameters:
A- the type of the first element - 第一个元素的类型B- the type of the second element - 第二个元素的类型- Parameters:
first- the first element (nullable) - 第一个元素(可为 null)second- the second element (nullable) - 第二个元素(可为 null)- Returns:
- a new Pair - 新的二元组
-
fromEntry
- Type Parameters:
A- the type of the key - 键的类型B- the type of the value - 值的类型- Parameters:
entry- the map entry - Map 条目- Returns:
- a new Pair containing the entry's key and value - 包含条目键值的新二元组
- Throws:
NullPointerException- if entry is null - 如果 entry 为 null
-
getKey
-
getValue
-
setValue
Always throwsUnsupportedOperationExceptionsince Pair is immutable. 始终抛出UnsupportedOperationException,因为 Pair 是不可变的。- Specified by:
setValuein interfaceMap.Entry<A,B> - Parameters:
value- ignored - 忽略- Returns:
- never returns - 永远不返回
- Throws:
UnsupportedOperationException- always - 始终抛出
-
swap
-
mapFirst
Transforms the first element using the given function. 使用给定函数变换第一个元素。- Type Parameters:
C- the type of the new first element - 新的第一个元素的类型- Parameters:
fn- the mapping function - 映射函数- Returns:
- a new Pair with the transformed first element - 变换了第一个元素的新二元组
- Throws:
NullPointerException- if fn is null - 如果 fn 为 null
-
mapSecond
Transforms the second element using the given function. 使用给定函数变换第二个元素。- Type Parameters:
C- the type of the new second element - 新的第二个元素的类型- Parameters:
fn- the mapping function - 映射函数- Returns:
- a new Pair with the transformed second element - 变换了第二个元素的新二元组
- Throws:
NullPointerException- if fn is null - 如果 fn 为 null
-
map
Applies a bi-function to both elements and returns the result. 对两个元素应用双参数函数并返回结果。- Type Parameters:
C- the result type - 结果类型- Parameters:
fn- the mapping function - 映射函数- Returns:
- the result of applying the function - 函数应用的结果
- Throws:
NullPointerException- if fn is null - 如果 fn 为 null
-
toString
-
first
-
second
-