Java 中的 JavaTuples
    
    Rupam Yadav
    2023年10月12日
    
    Java
    Java Tuple
    
 
元组是一种数据结构,可以存储对象而不需要它们之间的任何关系,但具有共同的动机。Java 不支持元组数据结构,但我们可以使用 JavaTuples 库。
JavaTuples 的特点
    
- 元组是类型安全的
- 它们是不可变的
- 我们可以遍历元组值
- 它们是可序列化的
- 他们实现了 Comparable<Tuple>
- 实现诸如 equals()、hashCode()和toString()之类的方法
JavaTuples 中的元组类
我们最多可以在元组类中使用十个元素。JavaTuples 中的每个元素都有特殊的子类,如下表所述:
| 元素数量(元组大小) | 类名称 | 例子 | 
|---|---|---|
| 一 | Unit | Unit<1> | 
| 二 | Pair | Pair<1,2> | 
| 三 | Triplet | Triplet<1,2,3> | 
| 四 | Quartet | Quartet<1,2,3,4> | 
| 五 | Quintet | Quintet<1,2,3,4,5> | 
| 六 | Sextet | Sextet<1,2,3,4,5,6> | 
| 七 | Septet | Septet<1,2,3,4,5,6,7> | 
| 八 | Octet | Octet<1,2,3,4,5,6,7,8> | 
| 九 | Ennead | Ennead<1,2,3,4,5,6,7,8,9> | 
| 十 | Decade | Decade<1,2,3,4,5,6,7,8,9,10> | 
JavaTuples 中的方法
Tuple 类提供了各种方法来执行操作。
| 方法 | 描述 | 
|---|---|
| equals() | 它用于覆盖 Object类的 equals 方法的默认实现 | 
| hashCode() | 返回每个对象的哈希码。 | 
| toString() | 返回一个字符串表示 | 
| getSize() | 返回元组的大小 | 
| getValue() | 返回元组中指定位置的值 | 
| indexOf() | 返回给定字符串中询问的子字符串的索引。 | 
| contains() | 检查元组中是否存在元素 | 
| containsAll() | 检查列表或数组的所有元素是否存在于元组中 | 
| iterator() | 返回一个迭代器 | 
| lastIndexOf() | 返回给定字符串中询问的子字符串的最后一个索引。 | 
| toArray() | 元组到数组 | 
| toList() | 要列出的元组 | 
在 Java 中实现元组
我们导入以下基于 maven 的依赖项以使用 JavaTuples 库。
   <dependency>
      <groupId>org.javatuples</groupId>
      <artifactId>javatuples</artifactId>
      <version>[1.2]</version>
      <scope>compile</scope>
   </dependency>
在示例中,我们实现了上表中的各种元组类,并通过打印它们来获取它们的值。我们在元组中提供不同的数据类型,如 String、Integer、Boolean 等。
我们还创建了一个类 DummyClass,如果它抛出错误,则在元组中传递它的对象。
import org.javatuples.*;
public class JavaTuplesExample {
  public static void main(String[] args) {
    Unit<String> unitTuple = new Unit<>("First Tuple Element");
    Pair<String, Integer> pairTuple = new Pair<>("First Tuple Element", 2);
    Triplet<String, Integer, Boolean> tripletTuple = new Triplet<>("First Tuple Element", 2, true);
    Quartet<String, Integer, Boolean, String> quartetTuple =
        new Quartet<>("First Tuple Element", 2, true, "Fourth Tuple Element");
    Quintet<String, Integer, Boolean, String, Integer> quintetTuple =
        new Quintet<>("First Tuple Element", 2, true, "Fourth Tuple Element", 5);
    Sextet<String, Integer, Boolean, String, Integer, DummyClass> sextetTuple =
        new Sextet<>("First Tuple Element", 2, true, "Fourth Tuple Element", 5, new DummyClass());
    Septet<String, Integer, Boolean, String, Integer, DummyClass, Integer> septetTuple =
        new Septet<>(
            "First Tuple Element", 2, true, "Fourth Tuple Element", 5, new DummyClass(), 7);
    Octet<String, Integer, Boolean, String, Integer, DummyClass, Integer, String> octetTuple =
        new Octet<>("First Tuple Element", 2, true, "Fourth Tuple Element", 5, new DummyClass(), 7,
            "Eight Tuple Element");
    Ennead<String, Integer, Boolean, String, Integer, DummyClass, Integer, String, Integer>
        enneadTuple = new Ennead<>("First Tuple Element", 2, true, "Fourth Tuple Element", 5,
            new DummyClass(), 7, "Eight Tuple Element", 9);
    Decade<String, Integer, Boolean, String, Integer, DummyClass, Integer, String, Integer, String>
        decadeTuple = new Decade<>("First Tuple Element", 2, true, "Fourth Tuple Element", 5,
            new DummyClass(), 7, "Eight Tuple Element", 9, "Tenth Tuple Element");
    System.out.println(unitTuple);
    System.out.println(pairTuple);
    System.out.println(tripletTuple);
    System.out.println(quartetTuple);
    System.out.println(quintetTuple);
    System.out.println(sextetTuple);
    System.out.println(septetTuple);
    System.out.println(octetTuple);
    System.out.println(enneadTuple);
    System.out.println(decadeTuple);
  }
}
class DummyClass {}
输出:
[First Tuple Element]
[First Tuple Element, 2]
[First Tuple Element, 2, true]
[First Tuple Element, 2, true, Fourth Tuple Element]
[First Tuple Element, 2, true, Fourth Tuple Element, 5]
[First Tuple Element, 2, true, Fourth Tuple Element, 5, DummyClass@254989ff]
[First Tuple Element, 2, true, Fourth Tuple Element, 5, DummyClass@5d099f62, 7]
[First Tuple Element, 2, true, Fourth Tuple Element, 5, DummyClass@37f8bb67, 7, Eight Tuple Element]
[First Tuple Element, 2, true, Fourth Tuple Element, 5, DummyClass@49c2faae, 7, Eight Tuple Element, 9]
[First Tuple Element, 2, true, Fourth Tuple Element, 5, DummyClass@20ad9418, 7, Eight Tuple Element, 9, Tenth Tuple Element]
        Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
    
作者: Rupam Yadav
    Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.
LinkedIn