Record Class Triple<A,B,C>

java.lang.Object
java.lang.Record
cloud.opencode.base.collections.Triple<A,B,C>
Type Parameters:
A - the type of the first element - 第一个元素的类型
B - the type of the second element - 第二个元素的类型
C - the type of the third element - 第三个元素的类型
Record Components:
first - the first element - 第一个元素
second - the second element - 第二个元素
third - the third element - 第三个元素
All Implemented Interfaces:
Serializable

public record Triple<A,B,C>(A first, B second, C third) extends Record implements Serializable
Triple - Immutable generic triple of three values Triple - 不可变的泛型三元组

A simple container holding three related values. Provides functional transformation methods and conversion to Pair.

一个持有三个相关值的简单容器。提供函数式变换方法以及到 Pair 的转换。

Features | 主要功能:

  • Immutable record-based implementation - 基于 record 的不可变实现
  • Functional transformations (mapFirst, mapSecond, mapThird) - 函数式变换
  • Projection to Pair (dropFirst, dropSecond, dropThird) - 投影到 Pair
  • Null-safe: allows null values - 空值安全:允许 null 值

Usage Examples | 使用示例:

// Create a triple - 创建三元组
Triple<String, Integer, Boolean> triple = Triple.of("name", 25, true);

// Map individual elements - 映射单个元素
Triple<String, String, Boolean> mapped = triple.mapSecond(String::valueOf);

// Drop to pair - 投影到二元组
Pair<String, Integer> pair = triple.dropThird(); // ("name", 25)
Since:
JDK 25, opencode-base-collections V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Triple(A first, B second, C third)
    Creates an instance of a Triple record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns a Pair containing the second and third elements, dropping the first.
    Returns a Pair containing the first and third elements, dropping the second.
    Returns a Pair containing the first and second elements, dropping the third.
    final boolean
    Indicates whether some other object is "equal to" this one.
    Returns the value of the first record component.
    final int
    Returns a hash code value for this object.
    <D> Triple<D,B,C>
    mapFirst(Function<? super A, ? extends D> fn)
    Transforms the first element using the given function.
    <D> Triple<A,D,C>
    mapSecond(Function<? super B, ? extends D> fn)
    Transforms the second element using the given function.
    <D> Triple<A,B,D>
    mapThird(Function<? super C, ? extends D> fn)
    Transforms the third element using the given function.
    static <A,B,C> Triple<A,B,C>
    of(A first, B second, C third)
    Creates a new Triple with the given values.
    Returns the value of the second record component.
    Returns the value of the third record component.
    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

    • Triple

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

    • of

      public static <A,B,C> Triple<A,B,C> of(A first, B second, C third)
      Creates a new Triple with the given values. 使用给定的值创建新的三元组。
      Type Parameters:
      A - the type of the first element - 第一个元素的类型
      B - the type of the second element - 第二个元素的类型
      C - the type of the third element - 第三个元素的类型
      Parameters:
      first - the first element (nullable) - 第一个元素(可为 null)
      second - the second element (nullable) - 第二个元素(可为 null)
      third - the third element (nullable) - 第三个元素(可为 null)
      Returns:
      a new Triple - 新的三元组
    • mapFirst

      public <D> Triple<D,B,C> mapFirst(Function<? super A, ? extends D> fn)
      Transforms the first element using the given function. 使用给定函数变换第一个元素。
      Type Parameters:
      D - the type of the new first element - 新的第一个元素的类型
      Parameters:
      fn - the mapping function - 映射函数
      Returns:
      a new Triple with the transformed first element - 变换了第一个元素的新三元组
      Throws:
      NullPointerException - if fn is null - 如果 fn 为 null
    • mapSecond

      public <D> Triple<A,D,C> mapSecond(Function<? super B, ? extends D> fn)
      Transforms the second element using the given function. 使用给定函数变换第二个元素。
      Type Parameters:
      D - the type of the new second element - 新的第二个元素的类型
      Parameters:
      fn - the mapping function - 映射函数
      Returns:
      a new Triple with the transformed second element - 变换了第二个元素的新三元组
      Throws:
      NullPointerException - if fn is null - 如果 fn 为 null
    • mapThird

      public <D> Triple<A,B,D> mapThird(Function<? super C, ? extends D> fn)
      Transforms the third element using the given function. 使用给定函数变换第三个元素。
      Type Parameters:
      D - the type of the new third element - 新的第三个元素的类型
      Parameters:
      fn - the mapping function - 映射函数
      Returns:
      a new Triple with the transformed third element - 变换了第三个元素的新三元组
      Throws:
      NullPointerException - if fn is null - 如果 fn 为 null
    • dropThird

      public Pair<A,B> dropThird()
      Returns a Pair containing the first and second elements, dropping the third. 返回包含第一和第二个元素的二元组,丢弃第三个。
      Returns:
      a Pair of (first, second) - (first, second) 二元组
    • dropFirst

      public Pair<B,C> dropFirst()
      Returns a Pair containing the second and third elements, dropping the first. 返回包含第二和第三个元素的二元组,丢弃第一个。
      Returns:
      a Pair of (second, third) - (second, third) 二元组
    • dropSecond

      public Pair<A,C> dropSecond()
      Returns a Pair containing the first and third elements, dropping the second. 返回包含第一和第三个元素的二元组,丢弃第二个。
      Returns:
      a Pair of (first, third) - (first, third) 二元组
    • 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
    • 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.
    • 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
    • third

      public C third()
      Returns the value of the third record component.
      Returns:
      the value of the third record component