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

public record Pair<L,R>(L left, R right) extends Record implements 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
    Constructor
    Description
    Pair(L left, R right)
    Creates an instance of a Pair record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Checks if all values are non-null 检查是否都非 null
    <T> T
    apply(BiFunction<? super L, ? super R, ? extends T> function)
    Applies a bi-function 应用二元函数
    static <L,R> Pair<L,R>
    Creates an empty Pair 创建空二元组
    final boolean
    Indicates whether some other object is "equal to" this one.
    Gets the first element (alias for left) 获取第一个元素(左值的别名)
    static <K,V> Pair<K,V>
    fromEntry(Map.Entry<K,V> entry)
    Creates a Pair from Map.Entry 从 Map.Entry 创建二元组
    final int
    Returns a hash code value for this object.
    boolean
    Checks if it contains a null value 检查是否包含 null 值
    key()
    Gets the key (alias for left, for Map scenarios) 获取键(左值的别名,用于 Map 场景)
    Returns the value of the left record component.
    <T,U> Pair<T,U>
    map(Function<? super L, ? extends T> leftMapper, Function<? super R, ? extends U> rightMapper)
    Maps both left and right values 同时映射左右值
    <T> Pair<T,R>
    mapLeft(Function<? super L, ? extends T> mapper)
    Maps the left value 映射左值
    <T> Pair<L,T>
    mapRight(Function<? super R, ? extends T> mapper)
    Maps the right value 映射右值
    static <L,R> Pair<L,R>
    of(L left, R right)
    Creates a Pair 创建二元组
    Returns the value of the right record component.
    Gets the second element (alias for right) 获取第二个元素(右值的别名)
    Swaps left and right values 交换左右值
    Converts to an array 转换为数组
    Converts to Map.Entry 转换为 Map.Entry
    Returns a string representation of this record class.
    Gets the value (alias for right, for Map scenarios) 获取值(右值的别名,用于 Map 场景)

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Pair

      public Pair(L left, R right)
      Creates an instance of a Pair record class.
      Parameters:
      left - the value for the left record component
      right - the value for the right record component
  • Method Details

    • of

      public static <L,R> Pair<L,R> of(L left, R right)
      Creates a Pair 创建二元组
      Type Parameters:
      L - left value type | 左值类型
      R - right value type | 右值类型
      Parameters:
      left - left value | 左值
      right - right value | 右值
      Returns:
      the Pair | 二元组
    • fromEntry

      public static <K,V> Pair<K,V> fromEntry(Map.Entry<K,V> entry)
      Creates a Pair from Map.Entry 从 Map.Entry 创建二元组
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      entry - Map.Entry
      Returns:
      the Pair | 二元组
    • empty

      public static <L,R> Pair<L,R> empty()
      Creates an empty Pair 创建空二元组
      Type Parameters:
      L - left value type | 左值类型
      R - right value type | 右值类型
      Returns:
      an empty Pair | 空二元组
    • first

      public L first()
      Gets the first element (alias for left) 获取第一个元素(左值的别名)
      Returns:
      left value | 左值
    • second

      public R second()
      Gets the second element (alias for right) 获取第二个元素(右值的别名)
      Returns:
      right value | 右值
    • key

      public L key()
      Gets the key (alias for left, for Map scenarios) 获取键(左值的别名,用于 Map 场景)
      Returns:
      left value | 左值
    • value

      public R value()
      Gets the value (alias for right, for Map scenarios) 获取值(右值的别名,用于 Map 场景)
      Returns:
      right value | 右值
    • swap

      public Pair<R,L> swap()
      Swaps left and right values 交换左右值
      Returns:
      the swapped Pair | 交换后的二元组
    • mapLeft

      public <T> Pair<T,R> mapLeft(Function<? super L, ? extends T> mapper)
      Maps the left value 映射左值
      Type Parameters:
      T - new left type | 新左值类型
      Parameters:
      mapper - the mapper function | 映射函数
      Returns:
      a new Pair | 新二元组
    • mapRight

      public <T> Pair<L,T> mapRight(Function<? super R, ? extends T> mapper)
      Maps the right value 映射右值
      Type Parameters:
      T - new right type | 新右值类型
      Parameters:
      mapper - the mapper function | 映射函数
      Returns:
      a new Pair | 新二元组
    • 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

      public <T> T apply(BiFunction<? super L, ? super R, ? extends T> function)
      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

      public Map.Entry<L,R> toEntry()
      Converts to Map.Entry 转换为 Map.Entry
      Returns:
      Map.Entry
    • toArray

      public Object[] toArray()
      Converts to an array 转换为数组
      Returns:
      an array containing two elements | 包含两个元素的数组
    • toString

      public String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      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 with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • left

      public L left()
      Returns the value of the left record component.
      Returns:
      the value of the left record component
    • right

      public R right()
      Returns the value of the right record component.
      Returns:
      the value of the right record component