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>

public record Pair<A,B>(A first, B second) extends Record implements Map.Entry<A,B>, Serializable
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
    Constructor
    Description
    Pair(A first, B second)
    Creates an instance of a Pair record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Compares this Pair with another object for equality, compatible with the Map.Entry contract.
    Returns the value of the first record component.
    static <A,B> Pair<A,B>
    fromEntry(Map.Entry<A,B> entry)
    Creates a Pair from a Map.Entry.
    Returns the first element as the key.
    Returns the second element as the value.
    int
    Returns a hash code consistent with the Map.Entry contract: Objects.hashCode(getKey()) ^ Objects.hashCode(getValue()).
    <C> C
    map(BiFunction<? super A, ? super B, ? extends C> fn)
    Applies a bi-function to both elements and returns the result.
    <C> Pair<C,B>
    mapFirst(Function<? super A, ? extends C> fn)
    Transforms the first element using the given function.
    <C> Pair<A,C>
    mapSecond(Function<? super B, ? extends C> fn)
    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.
    Returns the value of the second record component.
    setValue(B value)
    Always throws UnsupportedOperationException since Pair is immutable.
    Returns a new Pair with the elements swapped.
    final String
    Returns a string representation of this record class.

    Methods inherited from class Object

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

    • Pair

      public Pair(A first, B second)
      Creates an instance of a Pair record class.
      Parameters:
      first - the value for the first record component
      second - the value for the second record component
  • Method Details

    • hashCode

      public int hashCode()
      Returns a hash code consistent with the Map.Entry contract: Objects.hashCode(getKey()) ^ Objects.hashCode(getValue()). 返回与 Map.Entry 契约一致的哈希码。
      Specified by:
      hashCode in interface Map.Entry<A,B>
      Specified by:
      hashCode in class Record
      Returns:
      the hash code | 哈希码
    • equals

      public boolean equals(Object o)
      Compares this Pair with another object for equality, compatible with the Map.Entry contract. Returns true if the other object is a Map.Entry with equal key and value. 将此 Pair 与另一个对象进行相等性比较,兼容 Map.Entry 契约。
      Specified by:
      equals in interface Map.Entry<A,B>
      Specified by:
      equals in class Record
      Parameters:
      o - the object to compare | 要比较的对象
      Returns:
      true if equal | 如果相等返回 true
    • of

      public static <A,B> Pair<A,B> of(A first, B second)
      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

      public static <A,B> Pair<A,B> fromEntry(Map.Entry<A,B> entry)
      Creates a Pair from a Map.Entry. 从 Map.Entry 创建二元组。
      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

      public A getKey()
      Returns the first element as the key. 返回第一个元素作为键。
      Specified by:
      getKey in interface Map.Entry<A,B>
      Returns:
      the first element - 第一个元素
    • getValue

      public B getValue()
      Returns the second element as the value. 返回第二个元素作为值。
      Specified by:
      getValue in interface Map.Entry<A,B>
      Returns:
      the second element - 第二个元素
    • setValue

      public B setValue(B value)
      Always throws UnsupportedOperationException since Pair is immutable. 始终抛出 UnsupportedOperationException,因为 Pair 是不可变的。
      Specified by:
      setValue in interface Map.Entry<A,B>
      Parameters:
      value - ignored - 忽略
      Returns:
      never returns - 永远不返回
      Throws:
      UnsupportedOperationException - always - 始终抛出
    • swap

      public Pair<B,A> swap()
      Returns a new Pair with the elements swapped. 返回一个交换了元素的新二元组。
      Returns:
      a new Pair with first and second swapped - 交换了第一和第二个元素的新二元组
    • mapFirst

      public <C> Pair<C,B> mapFirst(Function<? super A, ? extends C> fn)
      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

      public <C> Pair<A,C> mapSecond(Function<? super B, ? extends C> fn)
      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

      public <C> C map(BiFunction<? super A, ? super B, ? extends C> fn)
      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

      public final 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
    • first

      public A first()
      Returns the value of the first record component.
      Returns:
      the value of the first record component
    • second

      public B second()
      Returns the value of the second record component.
      Returns:
      the value of the second record component