Javatuples 简明教程

JavaTuples - Overview

Tuple

元组是对象序列,这些对象可能相同类型,也可能不同类型。请考虑以下示例 −

Tuple is a sequence of objects which may or may not be of same type. Consider the following example −

[12,"TutorialsPoint", java.sql.Connection@li757b]

上面的对象是一个包含三个元素的元组,一个整数、一个字符串和一个连接对象。

Above object is a tuple of three elements, an Integer, a string and a Connection Object.

JavaTuple

JavaTuples 是一个非常简单的库,它提供了十个不同的元组类,这些类足以处理大多数与元组相关的需求。

JavaTuples is a very simple library which offers ten different tuple classses which are sufficient to handle most of the tuple related requirements.

  1. Unit<A> - 1 element

  2. Pair<A,B> - 2 elements

  3. Triplet<A,B,C> - 3 elements

  4. Quartet<A,B,C,D> - 4 elements

  5. Quintet<A,B,C,D,E> - 5 elements

  6. Sextet<A,B,C,D,E,F> - 6 elements

  7. Septet<A,B,C,D,E,F,G> - 7 elements

  8. Octet<A,B,C,D,E,F,G,H> - 8 elements

  9. Ennead<A,B,C,D,E,F,G,H,I> - 9 elements

  10. Decade<A,B,C,D,E,F,G,H,I,J> - 10 elements

除了这些元组类之外,JavaTuples 还出于语义原因提供了另外两个类。

Apart from these tuple classes, JavaTuples also provides two additional classes for semantics sake.

  1. KeyValue<A,B>

  2. LabelValue<A,B>

所有元组类都是类型安全的并且是不可变的,并且实现了以下接口和方法。

All tuple classes are typesafe and immutable and implements following interfaces and methods.

  1. Iterable

  2. Serializable

  3. Comparable<Tuple>

  4. equals()

  5. hashCode()

  6. toString()

Tuple vs List/Array

列表或数组可以包含任意数量的元素,但每个元素必须相同类型,而元组只能包含特定数量的元素,可以有不同类型的元素,但仍然是类型安全的。

List or Array can contain any number of elements but each element must be of same type whereas tuples can contain only specific number of elements, can have different type of elements but still are typesafe.