Javatuples 简明教程

JavaTuples - Quick Guide

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.

JavaTuples - Environment Setup

Local Environment Setup

如果您仍然愿意为 Java 编程语言设置您的环境,则本节指导您如何在您的机器上下载并设置 Java。请按照下面提到的步骤设置环境。

If you are still willing to set up your environment for Java programming language, then this section guides you on how to download and set up Java on your machine. Please follow the steps mentioned below to set up the environment.

Java SE 可以从链接 Download Java 中免费获取。因此,您可以根据自己的操作系统下载版本。

Java SE is freely available from the link Download Java. So you download a version based on your operating system.

按照说明下载 Java 并运行 .exe 以在您的计算机上安装 Java。安装计算机上的 Java 后,您需要设置环境变量以指向正确的安装目录 −

Follow the instructions to download Java and run the .exe to install Java on your machine. Once you have installed Java on your machine, you would need to set environment variables to point to correct installation directories −

Setting up the Path for Windows 2000/XP

我们假设您已将 Java 安装在 c:\Program Files\java\jdk 目录中 −

We are assuming that you have installed Java in c:\Program Files\java\jdk directory −

  1. Right-click on 'My Computer' and select 'Properties'.

  2. Click on the 'Environment variables' button under the 'Advanced' tab.

  3. Now, alter the 'Path' variable so that it also contains the path to the Java executable. Example, if the path is currently set to 'C:\WINDOWS\SYSTEM32', then change your path to read 'C:\WINDOWS\SYSTEM32;c:\Program Files\java\jdk\bin'.

Setting up the Path for Windows 95/98/M

我们假设您已将 Java 安装在 c:\Program Files\java\jdk 目录中 −

We are assuming that you have installed Java in c:\Program Files\java\jdk directory −

  1. Edit the 'C:\autoexec.bat' file and add the following line at the end − 'SET PATH=%PATH%;C:\Program Files\java\jdk\bin'

Setting up the Path for Linux, UNIX, Solaris, FreeBS

环境变量 PATH 应设置为指向已安装 Java 二进制文件的位置。如果你在这方面遇到问题,请参阅 shell 文档。

Environment variable PATH should be set to point to where the Java binaries have been installed. Refer to your shell documentation if you have trouble doing this.

例如,如果您用 bash 作为您的 shell,则您将向您 '.bashrc: export PATH=/path/to/java:$PATH' 的末尾添加以下行

Example, if you use bash as your shell, then you would add the following line to the end of your '.bashrc: export PATH=/path/to/java:$PATH'

要编写 Java 程序,您需要一个文本编辑器。市场中有许多高级集成开发环境可用。但目前,您可以考虑以下选项之一 −

To write your Java programs, you need a text editor. There are many sophisticated IDEs available in the market. But for now, you can consider one of the following −

  1. Notepad − On Windows machine you can use any simple text editor like Notepad (Recommended for this tutorial), TextPad.

  2. Netbeans − It is a Java IDE that is open-source and free which can be downloaded from www.netbeans.org/index.html.

  3. Eclipse − It is also a Java IDE developed by the eclipse open-source community and can be downloaded from www.eclipse.org.

Download JavaTuples Archie

Maven Repository - JavaTuples 下载 JavaTuples jar 文件的最新版本。在本教程中,下载 javatuples-1.2.jar 并将其复制到 C:\> javatuples 文件夹。

Download the latest version of JavaTuples jar file from Maven Repository - JavaTuples. In this tutorial, javatuples-1.2.jar is downloaded and copied into C:\> javatuples folder.

OS

Archive name

Windows

javatuples-1.2.jar

Linux

javatuples-1.2.jar

Mac

javatuples-1.2.jar

Set JavaTuples Environment

JavaTuples 环境变量设置为指向 JavaTuples jar 所在的计算机上的基本目录位置。假设我们在各个操作系统上将 javatuples-1.2.jar 提取到 JavaTuples 文件夹,如下所示。

Set the JavaTuples environment variable to point to the base directory location where JavaTuples jar is stored on your machine. Assuming, we’ve extracted javatuples-1.2.jar in JavaTuples folder on various Operating Systems as follows.

OS

Output

Windows

Set the environment variable JavaTuples to C:\JavaTuples

Linux

export JavaTuples=/usr/local/JavaTuples

Mac

export JavaTuples=/Library/JavaTuples

Set CLASSPATH Variable

CLASSPATH 环境变量设置为指向 JavaTuples jar 位置。假设您在各个操作系统上将 javatuples-1.2.jar 存储在 JavaTuples 文件夹中,如下所示。

Set the CLASSPATH environment variable to point to the JavaTuples jar location. Assuming, you have stored javatuples-1.2.jar in JavaTuples folder on various Operating Systems as follows.

OS

Output

Windows

Set the environment variable CLASSPATH to %CLASSPATH%;%JavaTuples%\javatuples-1.2.jar;.;

Linux

export CLASSPATH=$CLASSPATH:$JavaTuples/javatuples-1.2.jar:.

Mac

export CLASSPATH=$CLASSPATH:$JavaTuples/javatuples-1.2.jar:.

JavaTuples - Create Tuples

可以使用多个选项使用 JavaTuple 类创建元组。以下是示例:

A tuple using JavaTuple classes can be created using multiple options. Following are the examples −

Using with() Methods

每个元组类都有一个带对应参数的 with() 方法。例如:

Each tuple class has a with() method with corresponding parameters. For example −

Pair<String, Integer> pair = Pair.with("Test", Integer.valueOf(5));
Triplet<String, Integer, Double> triplet = Triplet.with("Test", Integer.valueOf(5),
   Double.valueOf(32.1));

Using Constructor

每个元组类都有一个带对应参数的构造函数。例如:

Each tuple class has a constructor with corresponding parameters. For example −

Pair<String, Integer> pair = new Pair("Test", Integer.valueOf(5));
Triplet<String, Integer, Double> triplet = new Triplet("Test", Integer.valueOf(5),
   Double.valueOf(32.1));

Using Collections

每个元组类都有一个带有相应参数的 fromCollection() 方法。例如:

Each tuple class has a fromCollection() method with corresponding parameters. For example −

Pair<String, Integer> pair = Pair.fromCollection(listOfTwoElements);

Using Iterable

每个元组类都有一个 fromIterable() 方法以通用方式获取元素。例如:

Each tuple class has a fromIterable() method to get elements in generic fashion. For example −

// Retrieve three values from an iterable starting at index 5
Triplet<Integer,Integer,Integer> triplet = Triplet.fromIterable(listOfInts, 5);

Example

让我们来看看 JavaTuples 在操作中的表现。此处我们将了解使用各种方式创建元组。

Let’s see JavaTuples in action. Here we’ll see how to create tupels using various ways.

C:&gt;JavaTuples 中创建一个名为 TupleTester 的 Java 类文件。

Create a java class file named TupleTester in C:\>JavaTuples.

文件:TupleTester.java

File: TupleTester.java

package com.tutorialspoint;
import java.util.ArrayList;
import java.util.List;
import org.javatuples.Pair;

public class TupleTester {
   public static void main(String args[]){
      //Create using with() method
      Pair<String, Integer> pair = Pair.with("Test", Integer.valueOf(5));
      //Create using constructor()
      Pair<String, Integer> pair1 = new Pair("Test", Integer.valueOf(5));
      List<Integer> listOfInts = new ArrayList<Integer>();
      listOfInts.add(1);
      listOfInts.add(2);
      //Create using fromCollection() method
      Pair<Integer, Integer> pair2 = Pair.fromCollection(listOfInts);
      listOfInts.add(3);
      listOfInts.add(4);
      listOfInts.add(5);
      listOfInts.add(6);
      listOfInts.add(8);
      listOfInts.add(9);
      listOfInts.add(10);
      listOfInts.add(11);
      //Create using fromIterable() method
      // Retrieve three values from an iterable starting at index 5
      Pair<Integer,Integer> pair3 = Pair.fromIterable(listOfInts, 5);
      //print all tuples
      System.out.println(pair);
      System.out.println(pair1);
      System.out.println(pair2);
      System.out.println(pair3);
   }
}

Verify the result

Verify the result

使用以下 javac 编译器编译类:

Compile the classes using javac compiler as follows −

C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java

现在运行 TupleTester 查看结果:

Now run the TupleTester to see the result −

C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester

Output

验证输出

Verify the Output

[Test, 5]
[Test, 5]
[1, 2]
[6, 8]

JavaTuples - Get Values

元组具有 getValueX() 方法来获取值,以及 getValue() 方法来通过索引获取值。例如,Triplet 类具有以下方法。

A tuple has getValueX() methods to get values and getValue() a generic method to get value by index. For example Triplet class has following methods.

  1. getValue(index) − returns value at index starting from 0.

  2. getValue0() − returns value at index 0.

  3. getValue1() − returns value at index 1.

  4. getValue2() − returns value at index 2.

Feature

  1. getValueX() methods are typesafe and no cast is required, but getValue(index) is generic.

  2. A tuple has getValueX() methods upto element count. For example, Triplet has no getValue3() method but Quartet has.

  3. Semantic Classes KeyValue and LabelValue has getKey()/getValue() and getLabel()/getValue() instead of getValue0()/getValue1() methods.

Example

让我们实际看看 JavaTuples。我们将了解如何使用各种方法从元组获取值。

Let’s see JavaTuples in action. Here we’ll see how to get values from a tuple using various ways.

C:&gt;JavaTuples 中创建一个名为 TupleTester 的 Java 类文件。

Create a java class file named TupleTester in C:\>JavaTuples.

文件:TupleTester.java

File: TupleTester.java

package com.tutorialspoint;
import org.javatuples.KeyValue;
import org.javatuples.Pair;
public class TupleTester {
   public static void main(String args[]){
      //Create using with() method
      Pair<String, Integer> pair = Pair.with("Test", Integer.valueOf(5));
      Object value0Obj = pair.getValue(0);
      Object value1Obj = pair.getValue(1);
      String value0 = pair.getValue0();
      Integer value1 = pair.getValue1();
      System.out.println(value0Obj);
      System.out.println(value1Obj);
      System.out.println(value0);
      System.out.println(value1);
	   KeyValue<String, Integer> keyValue = KeyValue.with(
         "Test", Integer.valueOf(5)
      );
      value0 = keyValue.getKey();
      value1 = keyValue.getValue();
      System.out.println(value0Obj);
      System.out.println(value1Obj);
   }
}

Verify the result

Verify the result

使用以下 javac 编译器编译类:

Compile the classes using javac compiler as follows −

C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java

现在运行 TupleTester 查看结果:

Now run the TupleTester to see the result −

C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester

Output

验证输出

Verify the Output

Test
5
Test
5
Test
5

JavaTuples - Set Values

元组具有 setAtX() 方法来设置特定索引处的值。例如,Triplet 类具有以下方法。

A tuple has setAtX() methods to set value at particular index. For example Triplet class has following methods.

  1. setAt0() − set value at index 0.

  2. setAt1() − set value at index 1.

  3. setAt2() − set value at index 2.

Feature

  1. Tuples are immutable. Each setAtX() returns a new tuple which is to be used to see the updated value.

  2. Type of a position of a tuple can be changed using setAtX() method.

Example

让我们实际看看 JavaTuples。我们将了解如何使用各种方法在元组中设置值。

Let’s see JavaTuples in action. Here we’ll see how to set values in a tuple using various ways.

C:&gt;JavaTuples 中创建一个名为 TupleTester 的 Java 类文件。

Create a java class file named TupleTester in C:\>JavaTuples.

文件:TupleTester.java

File: TupleTester.java

package com.tutorialspoint;
import org.javatuples.Pair;
public class TupleTester {
   public static void main(String args[]){
      //Create using with() method
      Pair<String, Integer> pair = Pair.with("Test", Integer.valueOf(5));
      Pair<String, Integer> pair1 = pair.setAt0("Updated Value");
      System.out.println("Original Pair: " + pair);
      System.out.println("Updated Pair:" + pair1);
      Pair<String, String> pair2 = pair.setAt1("Changed Type");
      System.out.println("Original Pair: " + pair);
      System.out.println("Changed Pair:" + pair2);
   }
}

Verify the result

Verify the result

使用以下 javac 编译器编译类:

Compile the classes using javac compiler as follows −

C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java

现在运行 TupleTester 查看结果:

Now run the TupleTester to see the result −

C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester

Output

验证输出

Verify the Output

Original Pair: [Test, 5]
Updated Pair:[Updated Value, 5]
Original Pair: [Test, 5]
Changed Pair:[Test, Changed Type]

JavaTuples - Add Elements

元组在元组的末尾具有 add() 方法,它也会更改元组的类型。例如,向 Triplet 元组中添加一个元素会将其转换为 Quartet 元组。

A tuple has add() method at the end of a tuple and it changes the type of tuple as well. For example adding a element to Triplet tuple will convert it to a Quartet tuple.

Quartet<String,String,String,String> quartet = triplet.add("Test");

元组还具有 addAtX() 方法,用于从 0 开始在特定索引处添加位置。

A tuple has addAtX() methods as well to add a position at particular index starting from 0.

Quartet<String,String,String,String> quartet = triplet.addAt1("Test");

元组可以使用 addAtX() 方法添加多个元素。

A tuple can add more than one elements using addAtX() methods.

Quartet<String,String,String,String> quartet = pair.addAt1("Test1", "Test2");

元组也可以使用addAtX()方法来添加一个元组。

A tuple can add a tuple as well using addAtX() methods.

Quartet<String,String,String,String> quartet = pair.addAt1(pair1);

Example

让我们来看看 JavaTuples 的实际应用。这里我们将看到如何使用各种方法在元组中添加值。

Let’s see JavaTuples in action. Here we’ll see how to add values in a tuple using various ways.

C:&gt;JavaTuples 中创建一个名为 TupleTester 的 Java 类文件。

Create a java class file named TupleTester in C:\>JavaTuples.

文件:TupleTester.java

File: TupleTester.java

package com.tutorialspoint;
import org.javatuples.Pair;
import org.javatuples.Quartet;
import org.javatuples.Quintet;
import org.javatuples.Triplet;
public class TupleTester {
   public static void main(String args[]){
      Pair<String, Integer> pair = Pair.with("Test", Integer.valueOf(5));
      Triplet<String, Integer, String> triplet = pair.add("Test2");
      Quartet<String, String, Integer, String> quartet = triplet.addAt1("Test1");
      Quintet<String, Integer, String, String, Integer> quintet = triplet.add(pair);
      System.out.println("Pair: " + pair);
      System.out.println("Triplet:" + triplet);
      System.out.println("Quartet:" + quartet);
      System.out.println("Quintet:" + quintet);
   }
}

Verify the result

Verify the result

使用以下 javac 编译器编译类:

Compile the classes using javac compiler as follows −

C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java

现在运行 TupleTester 查看结果:

Now run the TupleTester to see the result −

C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester

Output

验证输出

Verify the Output

Pair: [Test, 5]
Triplet:[Test, 5, Test2]
Quartet:[Test, Test1, 5, Test2]
Quintet:[Test, 5, Test2, Test, 5]

JavaTuples - Remove Elements

元组有removeAtX()方法来移除特定索引处的值。例如,Triplet 类有以下方法。

A tuple has removeAtX() methods to remove value at particular index. For example Triplet class has following methods.

  1. removeAt0() − remove value at index 0 and return the resulted tuple.

  2. removeAt1() − remove value at index 1 and return the resulted tuple.

  3. removeAt2() − remove value at index 2 and return the resulted tuple.

移除一个元素会返回一个新的元组。

Removing an element returns a new tuple.

Example

让我们来看看 JavaTuples 的实际应用。这里我们将看到如何在元组中移除值。

Let’s see JavaTuples in action. Here we’ll see how to remove value in a tuple.

C:&gt;JavaTuples 中创建一个名为 TupleTester 的 Java 类文件。

Create a java class file named TupleTester in C:\>JavaTuples.

文件:TupleTester.java

File: TupleTester.java

package com.tutorialspoint;
import org.javatuples.Pair;
import org.javatuples.Triplet;
public class TupleTester {
   public static void main(String args[]){
      Triplet<String, Integer, String> triplet = Triplet.with(
         "Test1", Integer.valueOf(5), "Test2"
      );
      Pair<String, Integer> pair = triplet.removeFrom2();
      System.out.println("Triplet:" + triplet);
      System.out.println("Pair: " + pair);
   }
}

Verify the result

Verify the result

使用以下 javac 编译器编译类:

Compile the classes using javac compiler as follows −

C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java

现在运行 TupleTester 查看结果:

Now run the TupleTester to see the result −

C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester

Output

验证输出

Verify the Output

Triplet:[Test1, 5, Test2]
Pair: [Test1, 5]

JavaTuples - Conversion

Tuple to List/Array

元组可以转换成 List/Array,但会损失类型安全性,转换后的列表是 List<Object>/Object[] 类型。

A tuple can be converted to List/Array but at cost of type safety and converted list is of type List<Object>/Object[].

List<Object> list = triplet.toList();
Object[] array = triplet.toArray();

Collection/Array to Tuple

集合可以使用fromCollection()方法转换成元组,数组可以使用fromArray()方法转换成元组。

A collection can be converted to tuple using fromCollection() method and array can be converted to tuple using fromArray() method.

Pair<String, Integer> pair = Pair.fromCollection(list);
Quartet<String,String,String,String> quartet = Quartet.fromArray(array);

如果数组/集合的大小与元组不同,则会发生 IllegalArgumentException。

If size of array/collection is different than that of tuple, then IllegalArgumentException will occur.

Exception in thread "main" java.lang.IllegalArgumentException:
Array must have exactly 4 elements in order to create a Quartet. Size is 5
   at ...

Example

让我们来看看 JavaTuples 的实际应用。这里我们将看到如何将元组转换成列表/数组,反之亦然。

Let’s see JavaTuples in action. Here we’ll see how to convert tuple to list/array and vice versa.

C:&gt;JavaTuples 中创建一个名为 TupleTester 的 Java 类文件。

Create a java class file named TupleTester in C:\>JavaTuples.

文件:TupleTester.java

File: TupleTester.java

package com.tutorialspoint;
import java.util.List;
import org.javatuples.Quartet;
import org.javatuples.Triplet;
public class TupleTester {
   public static void main(String args[]){
      Triplet<String, Integer, String> triplet = Triplet.with(
         "Test1", Integer.valueOf(5), "Test2"
      );
      List<Object> list = triplet.toList();
      Object[] array = triplet.toArray();
      System.out.println("Triplet:" + triplet);
      System.out.println("List: " + list);
      System.out.println();
      for(Object object: array) {
         System.out.print(object + " " );
      }
      System.out.println();
      String[] strArray = new String[] {"a", "b" , "c" , "d"};
      Quartet<String, String, String, String> quartet = Quartet.fromArray(strArray);
      System.out.println("Quartet:" + quartet);
   }
}

Verify the result

Verify the result

使用以下 javac 编译器编译类:

Compile the classes using javac compiler as follows −

C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java

现在运行 TupleTester 查看结果:

Now run the TupleTester to see the result −

C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester

Output

验证输出

Verify the Output

Triplet:[Test1, 5, Test2]
List: [Test1, 5, Test2]

Test1 5 Test2
Quartet:[a, b, c, d]

JavaTuples - Iteration

每个元组都实现了 Iterable 接口,可以像集合一样迭代。

Each tuple implements Iterable interface and can be iterated in similar fashion as collection.

Pair<String, Integer> pair = Pair.with("Test", Integer.valueOf(5));
for(Object object: Pair){
	System.out.println(object);
}

Example

让我们来看看 JavaTuples 的实际应用。这里我们将看到如何迭代元组。

Let’s see JavaTuples in action. Here we’ll see how to iterate tuples.

C:&gt;JavaTuples 中创建一个名为 TupleTester 的 Java 类文件。

Create a java class file named TupleTester in C:\>JavaTuples.

文件:TupleTester.java

File: TupleTester.java

package com.tutorialspoint;
import org.javatuples.Quartet;
import org.javatuples.Triplet;
public class TupleTester {
   public static void main(String args[]){
      Triplet<String, Integer, String> triplet = Triplet.with(
         "Test1", Integer.valueOf(5), "Test2"
      );
      for(Object object: triplet) {
         System.out.print(object + " " );
      }
      System.out.println();
      System.out.println(triplet);
      String[] strArray = new String[] {"a", "b" , "c" , "d"};
      Quartet<String, String, String, String> quartet = Quartet.fromArray(strArray);
      for(Object object: quartet) {
         System.out.print(object + " " );
      }
      System.out.println();
      System.out.println("Quartet:" + quartet);
   }
}

Verify the result

Verify the result

使用以下 javac 编译器编译类:

Compile the classes using javac compiler as follows −

C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java

现在运行 TupleTester 查看结果:

Now run the TupleTester to see the result −

C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester

Output

验证输出

Verify the Output

Test1 5 Test2
[Test1, 5, Test2]
a b c d
Quartet:[a, b, c, d]

JavaTuples - Checking Elements

每个元组都提供了公用方法来以类似于集合的方式检查其元素。

Each tuple provides utility methods to check their elements in similar fashion as collection.

  1. contains(element) − checks if element is present or not.

  2. containsAll(collection) − checks if elements are present or not.

  3. indexOf(element) − returns the index of first element if present otherwise -1.

  4. lastIndexOf(element) − returns the index of last element if present otherwise -1.

Pair<String, Integer> pair = Pair.with("Test", Integer.valueOf(5));
boolean isPresent = pair.contains("Test");

Example

我们来看一下 JavaTuples 的实际应用。我们将会看到如何检查元组中的元素。

Let’s see JavaTuples in action. Here we’ll see how to check elements in a tuple.

C:&gt;JavaTuples 中创建一个名为 TupleTester 的 Java 类文件。

Create a java class file named TupleTester in C:\>JavaTuples.

文件:TupleTester.java

File: TupleTester.java

package com.tutorialspoint;
import java.util.List;
import org.javatuples.Quartet;
public class TupleTester {
   public static void main(String args[]){
      Quartet<String, Integer, String, String> quartet = Quartet.with(
         "Test1", Integer.valueOf(5), "Test3", "Test3"
      );
      System.out.println(quartet);
      boolean isPresent = quartet.contains(5);
      System.out.println("5 is present: " + isPresent);
      isPresent = quartet.containsAll(List.of("Test1", "Test3"));
      System.out.println("Test1, Test3 are present: " + isPresent);
      int indexOfTest3 = quartet.indexOf("Test3");
      System.out.println("First Test3 is present at: " + indexOfTest3);
      int lastIndexOfTest3 = quartet.lastIndexOf("Test3");
      System.out.println("Last Test3 is present at: " + lastIndexOfTest3);
   }
}

Verify the result

Verify the result

使用以下 javac 编译器编译类:

Compile the classes using javac compiler as follows −

C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java

现在运行 TupleTester 查看结果:

Now run the TupleTester to see the result −

C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester

Output

验证输出

Verify the Output

[Test1, 5, Test3, Test3]
5 is present: true
Test1, Test3 are present: true
First Test3 is present at: 2
Last Test3 is present at: 3

JavaTuples - Unit Class

Introduction

org.javatuples.Unit 类表示一个包含单个元素的元组。

The org.javatuples.Unit class represents a Tuple with single element.

Class declaration

以下是 org.javatuples.Unit 类的声明:

Following is the declaration for org.javatuples.Unit class −

public final class Unit<A>
   extends Tuple
      implements IValue0<A>

Class constructors

Sr.No.

Constructor & Description

1

Unit(A value0) This creates a Unit Tuple.

Class Methods

Sr.No.

Method & Description

1

Pair add(Unit tuple) This method returns a Pair tuple. Similarly other methods to add tuples are available e.g. add(Pair tuple) returns Triplet and upto add(Ennead tuple) returns Decade tuple.

2

Pair add(X0 value) This method add a value to the tuple and returns a Pair tuple. Similarly other methods to add values are available e.g. add(X0 value0, X1 value1) returns Triplet and so on upto add() with nine parameters.

3

Pair addAt0(Unit value) This method add a Unit tuple at index 0 and returns a Pair tuple. Similarly other methods to add tuples are available e.g. addAt0(Pair value) returns Triplet and so on upto addAt0(Ennead). Other similar method are addAt1(Unit value) which add a unit at index0 and have similar methods upto addAt1(Ennead).

4

Pair addAt0(X0 value) This method add a value at index 0 and returns a Pair tuple. Similarly other methods to add values are available e.g. addAt0(X0 value0, X1 value1) returns Triplet and so on upto addAt0() with nine parameters. Other similar method are addAt1(X0 value) which add a value at index0 and have similar methods upto addAt1() with nine parameters.

5

static <X> Unit<X> fromArray(X[] array) Create tuple from array.

6

static <X> Unit<X> fromCollection(Collection<X> collection) Create tuple from collection.

7

static <X> Unit<X> fromIterable(Iterable<X> iterable) Create tuple from iterable.

8

static <X> Unit<X> fromIterable(Iterable<X> iterable, int index) Create tuple from iterable, starting from the specified index.

9

int getSize() Return the size of the tuple.

10

A getValue0() Return the value of the tuple.

11

<X> Unit<X> setAt0(X value) Set the value of the tuple.

12

static <A> Unit<A> with(A value0) Create the tuple using given value.

Methods inherite

此类从以下类中继承方法:

This class inherits methods from the following classes −

  1. org.javatuples.Tuple

  2. Object

Example

我们来看一下 Unit 类的实际应用。我们将会看到如何使用各种方法。

Let’s see Unit Class in action. Here we’ll see how to use various methods.

C:&gt;JavaTuples 中创建一个名为 TupleTester 的 Java 类文件。

Create a java class file named TupleTester in C:\>JavaTuples.

文件:TupleTester.java

File: TupleTester.java

package com.tutorialspoint;
import java.util.ArrayList;
import java.util.List;
import org.javatuples.Pair;
import org.javatuples.Unit;
public class TupleTester {
   public static void main(String args[]){
      Unit<Integer> unit = Unit.with(5);
      System.out.println(unit);
      boolean isPresent = unit.contains(5);
      System.out.println("5 is present: " + isPresent);
      List<Integer> list = new ArrayList<>();
      list.add(1);
      Pair<Integer, String> pair = unit.add("Test");
      System.out.println(pair);
      Integer value = unit.getValue0();
      System.out.println(value);
      Unit<Integer> unit1 = Unit.fromCollection(list);
      System.out.println(unit1);
   }
}

Verify the result

Verify the result

使用以下 javac 编译器编译类:

Compile the classes using javac compiler as follows −

C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java

现在运行 TupleTester 查看结果:

Now run the TupleTester to see the result −

C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester

Output

验证输出

Verify the Output

[5]
5 is present: true
[5, Test]
5
[1]

JavaTuples - Pair Class

Introduction

org.javatuples.Pair 类表示一个包含两个元素的元组。

The org.javatuples.Pair class represents a Tuple with two elements.

Class Declaration

以下是 org.javatuples.Pair 类的声明:

Following is the declaration for org.javatuples.Pair class −

public final class Pair<A,B>
   extends Tuple
      implements IValue0<A>, IValue1<B>

Class Constructor

Sr.No.

Constructor & Description

1

Pair(A value0, B value1) This creates a Pair Tuple.

Class Methods

Sr.No.

Method & Description

1

Triplet add(Unit tuple) This method returns a Triplet tuple. Similarly other methods to add tuples are available e.g. add(Pair tuple) returns Quartet and upto add(Octet tuple) returns Decade tuple.

2

Triplet add(X0 value) This method add a value to the tuple and returns a Triplet tuple. Similarly other methods to add values are available e.g. add(X0 value0, X1 value1) returns Quartet and so on upto add() with eight parameters.

3

Triplet addAt0(Unit value) This method add a Unit tuple at index 0 and returns a Triplet tuple. Similarly other methods to add tuples are available e.g. addAt0(Pair value) returns Quartet and so on upto addAt0(Octet). Other similar method are addAt1(Unit value) which add a unit at index0 and have similar methods upto addAt2(Octet).

4

Triplet addAt0(X0 value) This method add a value at index 0 and returns a Triplet tuple. Similarly other methods to add values are available e.g. addAt0(X0 value0, X1 value1) returns Quartet and so on upto addAt0() with eight parameters. Other similar method are addAt1(X0 value) which add a value at index0 and have similar methods upto addAt2() with eight parameters.

5

static <X> Pair<X,X> fromArray(X[] array) Create tuple from array.

6

static <X> Pair<X,X> fromCollection(Collection<X> collection) Create tuple from collection.

7

static <X> Pair<X,X> fromIterable(Iterable<X> iterable) Create tuple from iterable.

8

static <X> Pair<X,X> fromIterable(Iterable<X> iterable, int index) Create tuple from iterable, starting from the specified index.

9

int getSize() Return the size of the tuple.

10

A getValue0() Returns the value of the tuple at index 0. Similarly getValue1() returns the value at index 1.

11

Unit<B> removeFrom0() Return the tuple after removing value of the tuple at index 0. Similarly removeFrom1() returns the tuple after removing value of the tuple at index 1.

12

<X> Pair<X,B> setAt0(X value) Set the value of the tuple at index 0.

13

static <A,B> Pair<A,B> with(A value0, B value1) Create the tuple using given value.

Methods inherite

此类从以下类中继承方法:

This class inherits methods from the following classes −

  1. org.javatuples.Tuple

  2. Object

Example

让我们看看 Pair 类在实际中的使用。在此,我们将了解如何使用各种方法。

Let’s see Pair Class in action. Here we’ll see how to use various methods.

C:&gt;JavaTuples 中创建一个名为 TupleTester 的 Java 类文件。

Create a java class file named TupleTester in C:\>JavaTuples.

文件:TupleTester.java

File: TupleTester.java

package com.tutorialspoint;
import java.util.ArrayList;
import java.util.List;
import org.javatuples.Pair;
import org.javatuples.Triplet;
import org.javatuples.Unit;

public class TupleTester {
   public static void main(String args[]){
      Pair<Integer, Integer> pair = Pair.with(5,6);
      System.out.println(pair);
      boolean isPresent = pair.contains(5);
      System.out.println("5 is present: " + isPresent);
      List<Integer> list = new ArrayList<>();
      list.add(1);
      list.add(2);
      Triplet<Integer,Integer, String> triplet = pair.add("Test");
      System.out.println(triplet);
      Integer value = pair.getValue0();
      System.out.println(value);
      Unit<Integer> unit = pair.removeFrom0();
      System.out.println(unit);
      Pair<Integer, Integer> pair1 = Pair.fromCollection(list);
      System.out.println(pair1);
   }
}

Verify the result

Verify the result

使用以下 javac 编译器编译类:

Compile the classes using javac compiler as follows −

C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java

现在运行 TupleTester 查看结果:

Now run the TupleTester to see the result −

C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester

Output

验证输出

Verify the Output

[5, 6]
5 is present: true
[5, 6, Test]
5
[6]
[1, 2]

JavaTuples - Triplet Class

Introduction

org.javatuples.Triplet 类表示包含三个元素的元组。

The org.javatuples.Triplet class represents a Tuple with three elements.

Class Declaration

以下是 org.javatuples.Triplet 类的声明 -

Following is the declaration for org.javatuples.Triplet class −

public final class Triplet<A,B,C>
   extends Tuple
      implements IValue0<A>, IValue1<B>, IValue2<C>

Class Constructors

Sr.No.

Constructor & Description

1

Triplet(A value0, B value1, C value2) This creates a Triplet Tuple.

Class Methods

Sr.No.

Method & Description

1

Quartet add(Unit tuple) This method returns a Quartet tuple. Similarly other methods to add tuples are available e.g. add(Pair tuple) returns Quintet and upto add(Septet tuple) returns Decade tuple.

2

Quartet add(X0 value) This method add a value to the tuple and returns a Quartet tuple. Similarly other methods to add values are available e.g. add(X0 value0, X1 value1) returns Quintet and so on upto add() with seven parameters.

3

Quartet addAt0(Unit value) This method add a Unit tuple at index 0 and returns a Quartet tuple. Similarly other methods to add tuples are available e.g. addAt0(Pair value) returns Quintet and so on upto addAt0(Septet). Other similar method are addAt1(Unit value) which add a unit at index0 and have similar methods upto addAt2(Septet).

4

Quartet addAt0(X0 value) This method add a value at index 0 and returns a Quartet tuple. Similarly other methods to add values are available e.g. addAt0(X0 value0, X1 value1) returns Quintet and so on upto addAt0() with seven parameters. Other similar method are addAt1(X0 value) which add a value at index0 and have similar methods upto addAt2() with seven parameters.

5

static <X> Triplet<X,X,X> fromArray(X[] array) Create tuple from array.

6

static <X> Triplet<X,X,X> fromCollection(Collection<X> collection) Create tuple from collection.

7

static <X> Triplet<X,X,X> fromIterable(Iterable<X> iterable) Create tuple from iterable.

8

static <X> Triplet<X,X,X> fromIterable(Iterable<X> iterable, int index) Create tuple from iterable, starting from the specified index.

9

int getSize() Return the size of the tuple.

10

A getValue0() Returns the value of the tuple at index 0. Similarly getValue1() upto getValue2() returns the value at index 1 and so on.

11

Pair<B,C> removeFrom0() Return the tuple after removing value of the tuple at index 0. Similarly removeFrom1() upto removeFrom2() returns the tuple after removing value of the tuple at index 1 and so on.

12

<X> Triplet<X,B,C> setAt0(X value) Set the value of the tuple at index 0.

13

static <A> Triplet<A,B,C> with(A value0, B value1, C value2) Create the tuple using given value.

Methods inherite

此类从以下类中继承方法:

This class inherits methods from the following classes −

  1. org.javatuples.Tuple

  2. Object

Example

让我们看看 Triplet 类的实际应用。我们将在其中了解如何使用各种方法。

Let’s see Triplet Class in action. Here we’ll see how to use various methods.

C:&gt;JavaTuples 中创建一个名为 TupleTester 的 Java 类文件。

Create a java class file named TupleTester in C:\>JavaTuples.

文件:TupleTester.java

File: TupleTester.java

package com.tutorialspoint;
import java.util.ArrayList;
import java.util.List;
import org.javatuples.Pair;
import org.javatuples.Quartet;
import org.javatuples.Triplet;

public class TupleTester {
   public static void main(String args[]){
      Triplet<Integer, Integer, Integer> triplet = Triplet.with(5, 6, 7);
      System.out.println(triplet);
      boolean isPresent = triplet.contains(5);
      System.out.println("5 is present: " + isPresent);
      List<Integer> list = new ArrayList<>();
      list.add(1);
      list.add(2);
      list.add(3);
      Quartet<Integer, Integer, Integer, String> quartet = triplet.add("Test");
      System.out.println(quartet);
      Integer value = triplet.getValue0();
      System.out.println(value);
      Pair<Integer, Integer> pair = triplet.removeFrom0();
      System.out.println(pair);
      Triplet<Integer, Integer, Integer> triplet1 =
         Triplet.fromCollection(list);
      System.out.println(triplet1);
   }
}

Verify the result

Verify the result

使用以下 javac 编译器编译类:

Compile the classes using javac compiler as follows −

C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java

现在运行 TupleTester 查看结果:

Now run the TupleTester to see the result −

C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester

Output

验证输出

Verify the Output

[5, 6, 7]
5 is present: true
[5, 6, 7, Test]
5
[6, 7]
[1, 2, 3]

JavaTuples - Quartet Class

Introduction

org.javatuples.Quartet 类表示具有四个元素的元组。

The org.javatuples.Quartet class represents a Tuple with four elements.

Class Declaration

以下是 org.javatuples.Quartet 类的声明 −

Following is the declaration for org.javatuples.Quartet class −

public final class Quartet<A, B, C, D>
   extends Tuple
      implements IValue0<A>, IValue1<B>, IValue2<C>, IValue3<D>

Class Constructor

Sr.No.

Constructor & Description

1

Quartet(A value0, B value1, C value2, D value3) This creates a Quartet Tuple.

Class Methods

Sr.No.

Method & Description

1

Quintet add(Unit tuple) This method returns a Quintet tuple. Similarly other methods to add tuples are available e.g. add(Pair tuple) returns Sextet and upto add(Sextet tuple) returns Decade tuple.

2

Quintet add(X0 value) This method add a value to the tuple and returns a Quintet tuple. Similarly other methods to add values are available e.g. add(X0 value0, X1 value1) returns Sextet and so on upto add() with six parameters.

3

Quintet addAt0(Unit value) This method add a Unit tuple at index 0 and returns a Quintet tuple. Similarly other methods to add tuples are available e.g. addAt0(Pair value) returns Sextet and so on upto addAt0(Sextet). Other similar method are addAt1(Unit value) which add a unit at index0 and have similar methods upto addAt2(Sextet).

4

Quintet addAt0(X0 value) This method add a value at index 0 and returns a Quintet tuple. Similarly other methods to add values are available e.g. addAt0(X0 value0, X1 value1) returns Sextet and so on upto addAt0() with six parameters. Other similar method are addAt1(X0 value) which add a value at index0 and have similar methods upto addAt2() with six parameters.

5

static <X> Quartet<X,X,X,X> fromArray(X[] array) Create tuple from array.

6

static <X> Quartet<X,X,X,X> fromCollection(Collection<X> collection) Create tuple from collection.

7

static <X> Quartet<X,X,X,X> fromIterable(Iterable<X> iterable) Create tuple from iterable.

8

static <X> Quartet<X,X,X,X> fromIterable(Iterable<X> iterable, int index) Create tuple from iterable, starting from the specified index.

9

int getSize() Return the size of the tuple.

10

A getValue0() Returns the value of the tuple at index 0. Similarly getValue1() upto getValue3() returns the value at index 1 and so on.

11

Triplet<B,C,D> removeFrom0() Return the tuple after removing value of the tuple at index 0. Similarly removeFrom1() upto removeFrom3() returns the tuple after removing value of the tuple at index 1 and so on.

12

<X> Quartet<X,B,C,D> setAt0(X value) Set the value of the tuple at index 0.

13

static <A> Quartet<A,B,C,D> with(A value0, B value1, C value2, D value3) Create the tuple using given value.

Methods inherite

此类从以下类中继承方法:

This class inherits methods from the following classes −

  1. org.javatuples.Tuple

  2. Object

Example

我们来看看 Quartet 类的实际应用。在这里,我们将看到如何使用各种方法。

Let’s see Quartet Class in action. Here we’ll see how to use various methods.

C:&gt;JavaTuples 中创建一个名为 TupleTester 的 Java 类文件。

Create a java class file named TupleTester in C:\>JavaTuples.

文件:TupleTester.java

File: TupleTester.java

package com.tutorialspoint;
import java.util.ArrayList;
import java.util.List;
import org.javatuples.Quartet;
import org.javatuples.Quintet;
import org.javatuples.Triplet;

public class TupleTester {
   public static void main(String args[]){
      Quartet<Integer, Integer, Integer, Integer> quartet = Quartet.with(
         5, 6, 7,8
      );
      System.out.println(quartet);
      boolean isPresent = quartet.contains(5);
      System.out.println("5 is present: " + isPresent);
      List<Integer> list = new ArrayList<>();
      list.add(1);
      list.add(2);
      list.add(3);
      list.add(4);
      Quintet<Integer, Integer, Integer, Integer, String> quintet = quartet.add("Test");
      System.out.println(quintet);
      Integer value = quartet.getValue0();
      System.out.println(value);
      Triplet<Integer, Integer, Integer> triplet = quartet.removeFrom0();
      System.out.println(triplet);
      Quartet<Integer, Integer, Integer, Integer> quartet1 = Quartet.fromCollection(list);
      System.out.println(quartet1);
   }
}

Verify the result

Verify the result

使用以下 javac 编译器编译类:

Compile the classes using javac compiler as follows −

C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java

现在运行 TupleTester 查看结果:

Now run the TupleTester to see the result −

C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester

Output

验证输出

Verify the Output

[5, 6, 7, 8]
5 is present: true
[5, 6, 7, 8, Test]
5
[6, 7, 8]
[1, 2, 3, 4]

JavaTuples - Quintet Class

Introduction

org.javatuples.Quintet 类表示包含五个元素的元组。

The org.javatuples.Quintet class represents a Tuple with five elements.

Class Declaration

以下是 org.javatuples.Quintet 类的声明 −

Following is the declaration for org.javatuples.Quintet class −

public final class Quintet<A, B, C, D, E>
   extends Tuple
      implements IValue0<A>, IValue1<B>,
         IValue2<C>, IValue3<D>, IValue4<E>

Class Constructor

Sr.No.

Constructor & Description

1

Quintet(A value0, B value1, C value2, D value3, E value4) This creates a Quintet Tuple.

Class Methods

Sr.No.

Method & Description

1

Sextet add(Unit tuple) This method returns a Sextet tuple. Similarly other methods to add tuples are available e.g. add(Pair tuple) returns Septet and upto add(Quintet tuple) returns Decade tuple.

2

Sextet add(X0 value) This method add a value to the tuple and returns a Sextet tuple. Similarly other methods to add values are available e.g. add(X0 value0, X1 value1) returns Septet and so on upto add() with five parameters.

3

Sextet addAt0(Unit value) This method add a Unit tuple at index 0 and returns a Sextet tuple. Similarly other methods to add tuples are available e.g. addAt0(Pair value) returns Septet and so on upto addAt0(Quintet). Other similar method are addAt1(Unit value) which add a unit at index0 and have similar methods upto addAt4(Quintet).

4

Sextet addAt0(X0 value) This method add a value at index 0 and returns a Sextet tuple. Similarly other methods to add values are available e.g. addAt0(X0 value0, X1 value1) returns Septet and so on upto addAt0() with five parameters. Other similar method are addAt1(X0 value) which add a value at index0 and have similar methods upto addAt4() with five parameters.

5

static <X> Quintet<X,X,X,X,X> fromArray(X[] array) Create tuple from array.

6

static <X> Quintet<X,X,X,X,X> fromCollection(Collection<X> collection) Create tuple from collection.

7

static <X> Quintet<X,X,X,X,X> fromIterable(Iterable<X> iterable) Create tuple from iterable.

8

static <X> Quintet<X,X,X,X,X> fromIterable(Iterable<X> iterable, int index) Create tuple from iterable, starting from the specified index.

9

int getSize() Return the size of the tuple.

10

A getValue0() Returns the value of the tuple at index 0. Similarly getValue1() upto getValue4() returns the value at index 1 and so on.

11

Quartet<B,C,D,E> removeFrom0() Return the tuple after removing value of the tuple at index 0. Similarly removeFrom1() upto removeFrom4() returns the tuple after removing value of the tuple at index 1 and so on.

12

<X> Quintet<X,B,C,D,E> setAt0(X value) Set the value of the tuple at index 0.

13

static <A> Quintet<A,B,C,D,E> with(A value0, B value1, C value2, D value3, E value4) Create the tuple using given value.

Methods inherite

此类从以下类中继承方法:

This class inherits methods from the following classes −

  1. org.javatuples.Tuple

  2. Object

Example

我们来看看 Quintet 类的实际应用。这里我们将了解如何使用各种方法。

Let’s see Quintet Class in action. Here we’ll see how to use various methods.

C:&gt;JavaTuples 中创建一个名为 TupleTester 的 Java 类文件。

Create a java class file named TupleTester in C:\>JavaTuples.

文件:TupleTester.java

File: TupleTester.java

package com.tutorialspoint;
import java.util.ArrayList;
import java.util.List;
import org.javatuples.Quartet;
import org.javatuples.Quintet;
import org.javatuples.Sextet;
import org.javatuples.Triplet;

public class TupleTester {
   public static void main(String args[]){
      Quintet<Integer, Integer, Integer, Integer, Integer> quintet
         = Quintet.with(5, 6, 7,8,9);
      System.out.println(quintet);
      boolean isPresent = quintet.contains(5);
      System.out.println("5 is present: " + isPresent);
      List<Integer> list = new ArrayList<>();
      list.add(1);
      list.add(2);
      list.add(3);
      list.add(4);
      list.add(5);
      Sextet<Integer, Integer, Integer, Integer, Integer, String> sextet
         = quintet.add("Test");
      System.out.println(sextet);
      Integer value = quintet.getValue0();
      System.out.println(value);
      Quartet<Integer, Integer, Integer, Integer> quartet = quintet.removeFrom0();
      System.out.println(quartet);
      Quintet<Integer, Integer, Integer, Integer, Integer> quintet1
         = Quintet.fromCollection(list);
      System.out.println(quintet1);
   }
}

Verify the result

Verify the result

使用以下 javac 编译器编译类:

Compile the classes using javac compiler as follows −

C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java

现在运行 TupleTester 查看结果:

Now run the TupleTester to see the result −

C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester

Output

验证输出

Verify the Output

[5, 6, 7, 8, 9]
5 is present: true
[5, 6, 7, 8, 9, Test]
5
[6, 7, 8, 9]
[1, 2, 3, 4, 5]

JavaTuples - Sextet Class

Introduction

org.javatuples.Sextet 类表示一个包含 6 个元素的元组。

The org.javatuples.Sextet class represents a Tuple with six elements.

Class Declaration

以下是 org.javatuples.Sextet 类的声明:

Following is the declaration for org.javatuples.Sextet class −

public final class Sextet<A, B, C, D, E, F>
   extends Tuple
      implements IValue0<A>, IValue1<B>,
         IValue2<C>, IValue3<D>, IValue4<E>,
            IValue5<F>

Class Constructor

Sr.No.

Constructor & Description

1

Sextet(A value0, B value1, C value2, D value3, E value4, F value5) This creates a Sextet Tuple.

Class Methods

Sr.No.

Method & Description

1

Septet add(Unit tuple) This method returns a Septet tuple. Similarly other methods to add tuples are available e.g. add(Pair tuple) returns Octet and upto add(Quartet tuple) returns Decade tuple.

2

Septet add(X0 value) This method add a value to the tuple and returns a Septet tuple. Similarly other methods to add values are available e.g. add(X0 value0, X1 value1) returns Octet and so on upto add() with four parameters.

3

Septet addAt0(Unit value) This method add a Unit tuple at index 0 and returns a Septet tuple. Similarly other methods to add tuples are available e.g. addAt0(Pair value) returns Octet and so on upto addAt0(Quartet). Other similar method are addAt1(Unit value) which add a unit at index0 and have similar methods upto addAt5(Quartet).

4

Septet addAt0(X0 value) This method add a value at index 0 and returns a Septet tuple. Similarly other methods to add values are available e.g. addAt0(X0 value0, X1 value1) returns Octet and so on upto addAt0() with four parameters. Other similar method are addAt1(X0 value) which add a value at index0 and have similar methods upto addAt5() with four parameters.

5

static <X> Sextet<X,X,X,X,X,X> fromArray(X[] array) Create tuple from array.

6

static <X> Sextet<X,X,X,X,X,X> fromCollection(Collection<X> collection) Create tuple from collection.

7

static <X> Sextet<X,X,X,X,X,X> fromIterable(Iterable<X> iterable) Create tuple from iterable.

8

static <X> Sextet<X,X,X,X,X,X> fromIterable(Iterable<X> iterable, int index) Create tuple from iterable, starting from the specified index.

9

int getSize() Return the size of the tuple.

10

A getValue0() Returns the value of the tuple at index 0. Similarly getValue1() upto getValue5() returns the value at index 1 and so on.

11

Quintet<B,C,D,E,F> removeFrom0() Return the tuple after removing value of the tuple at index 0. Similarly removeFrom1() upto removeFrom5() returns the tuple after removing value of the tuple at index 1 and so on.

12

<X> Sextet<X,B,C,D,E,F> setAt0(X value) Set the value of the tuple at index 0.

13

static <A> Sextet<A,B,C,D,E,F> with(A value0, B value1, C value2, D value3, E value4, F value5) Create the tuple using given value.

Methods inherite

此类从以下类中继承方法:

This class inherits methods from the following classes −

  1. org.javatuples.Tuple

  2. Object

Example

让我们看看元组类正在行动。这里我们将看到如何使用各种方法。

Let’s see Sextet Class in action. Here we’ll see how to use various methods.

C:&gt;JavaTuples 中创建一个名为 TupleTester 的 Java 类文件。

Create a java class file named TupleTester in C:\>JavaTuples.

文件:TupleTester.java

File: TupleTester.java

package com.tutorialspoint;
import java.util.ArrayList;
import java.util.List;
import org.javatuples.Quartet;
import org.javatuples.Quintet;
import org.javatuples.Sextet;
import org.javatuples.Septet;
public class TupleTester {
   public static void main(String args[]){
      Sextet<Integer, Integer, Integer, Integer, Integer,Integer> sextet
         = Sextet.with(5, 6, 7,8,9,10);
      System.out.println(sextet);
      boolean isPresent = sextet.contains(5);
      System.out.println("5 is present: " + isPresent);
      List<Integer> list = new ArrayList<>();
      list.add(1);
      list.add(2);
      list.add(3);
      list.add(4);
      list.add(5);
      list.add(6);
      Septet<Integer, Integer, Integer, Integer, Integer, Integer, String> septet
         = sextet.add("Test");
      System.out.println(septet);
      Integer value = sextet.getValue0();
      System.out.println(value);
      Quintet<Integer, Integer, Integer, Integer,Integer> quintet
         = sextet.removeFrom0();
      System.out.println(quintet);
      Sextet<Integer, Integer, Integer, Integer, Integer,Integer> sextet1
         = Sextet.fromCollection(list);
      System.out.println(sextet1);
   }
}

Verify the result

Verify the result

使用以下 javac 编译器编译类:

Compile the classes using javac compiler as follows −

C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java

现在运行 TupleTester 查看结果:

Now run the TupleTester to see the result −

C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester

Output

验证输出

Verify the Output

[5, 6, 7, 8, 9, 10]
5 is present: true
[5, 6, 7, 8, 9, 10, Test]
5
[6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6]

JavaTuples - Septet Class

Introduction

org.javatuples.Septet 类表示一个具有七个元素的元组。

The org.javatuples.Septet class represents a Tuple with seven elements.

Class Declaration

以下是 org.javatuples.Septet 类的声明 −

Following is the declaration for org.javatuples.Septet class −

public final class Septet<A, B, C, D, E, F, G>
   extends Tuple
      implements IValue0<A>, IValue1<B>,
         IValue2<C>, IValue3<D>, IValue4<E>,
            IValue5<F>, IValue6<G>

Class Constructor

Sr.No.

Constructor & Description

1

Septet(A value0, B value1, C value2, D value3, E value4, F value5, G value6) This creates a Septet Tuple.

Class Methods

Sr.No.

Method & Description

1

Octet add(Unit tuple) This method returns a Octet tuple. Similarly other methods to add tuples are available e.g. add(Pair tuple) returns Ennead and upto add(Triplet tuple) returns Decade tuple.

2

Octet add(X0 value) This method add a value to the tuple and returns a Octet tuple. Similarly other methods to add values are available e.g. add(X0 value0, X1 value1) returns Ennead and so on upto add() with three parameters.

3

Octet addAt0(Unit value) This method add a Unit tuple at index 0 and returns a Octet tuple. Similarly other methods to add tuples are available e.g. addAt0(Pair value) returns Ennead and so on upto addAt0(Triplet). Other similar method are addAt1(Unit value) which add a unit at index0 and have similar methods upto addAt6(Triplet).

4

Octet addAt0(X0 value) This method add a value at index 0 and returns a Octet tuple. Similarly other methods to add values are available e.g. addAt0(X0 value0, X1 value1) returns Ennead and so on upto addAt0() with three parameters. Other similar method are addAt1(X0 value) which add a value at index0 and have similar methods upto addAt6() with three parameters.

5

static <X> Septet<X,X,X,X,X,X,X> fromArray(X[] array) Create tuple from array.

6

static <X> Septet<X,X,X,X,X,X,X> fromCollection(Collection<X> collection) Create tuple from collection.

7

static <X> Septet<X,X,X,X,X,X,X> fromIterable(Iterable<X> iterable) Create tuple from iterable.

8

static <X> Septet<X,X,X,X,X,X,X> fromIterable(Iterable<X> iterable, int index) Create tuple from iterable, starting from the specified index.

9

int getSize() Return the size of the tuple.

10

A getValue0() Returns the value of the tuple at index 0. Similarly getValue1() upto getValue6() returns the value at index 1 and so on.

11

Sextet<B,C,D,E,F,G> removeFrom0() Return the tuple after removing value of the tuple at index 0. Similarly removeFrom1() upto removeFrom6() returns the tuple after removing value of the tuple at index 1 and so on.

12

<X> Septet<X,B,C,D,E,F,G> setAt0(X value) Set the value of the tuple at index 0.

13

static <A> Septet<A,B,C,D,E,F,G> with(A value0, B value1, C value2, D value3, E value4, F value5, G value6) Create the tuple using given value.

Methods inherite

此类从以下类中继承方法:

This class inherits methods from the following classes −

  1. org.javatuples.Tuple

  2. Object

Example

让我们看看 Septet 类如何运作。在此,我们将看到如何使用各种方法。

Let’s see Septet Class in action. Here we’ll see how to use various methods.

C:&gt;JavaTuples 中创建一个名为 TupleTester 的 Java 类文件。

Create a java class file named TupleTester in C:\>JavaTuples.

文件:TupleTester.java

File: TupleTester.java

package com.tutorialspoint;
import java.util.ArrayList;
import java.util.List;
import org.javatuples.Septet;
import org.javatuples.Sextet;
import org.javatuples.Octet;
public class TupleTester {
   public static void main(String args[]){
      Septet<Integer, Integer, Integer, Integer, Integer,Integer,Integer> septet
         = Septet.with(5, 6, 7,8,9,10,11);
      System.out.println(septet);
      boolean isPresent = septet.contains(5);
      System.out.println("5 is present: " + isPresent);
      List<Integer> list = new ArrayList<>();
      list.add(1);
      list.add(2);
      list.add(3);
      list.add(4);
      list.add(5);
      list.add(6);
      list.add(7);
      Octet<Integer, Integer, Integer, Integer, Integer, Integer, Integer, String> octet
         = septet.add("Test");
      System.out.println(octet);
      Integer value = septet.getValue0();
      System.out.println(value);
      Sextet<Integer, Integer, Integer, Integer,Integer, Integer> sextet
         = septet.removeFrom0();
      System.out.println(sextet);
      Septet<Integer, Integer, Integer, Integer, Integer,Integer, Integer> septet1
         = Septet.fromCollection(list);
      System.out.println(septet1);
   }
}

Verify the result

Verify the result

使用以下 javac 编译器编译类:

Compile the classes using javac compiler as follows −

C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java

现在运行 TupleTester 查看结果:

Now run the TupleTester to see the result −

C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester

Output

验证输出

Verify the Output

[5, 6, 7, 8, 9, 10, 11]
5 is present: true
[5, 6, 7, 8, 9, 10, 11, Test]
5
[6, 7, 8, 9, 10, 11]
[1, 2, 3, 4, 5, 6, 7]

JavaTuples - Octet Class

Introduction

org.javatuples.Octet 类表示包含八个元素的元组。

The org.javatuples.Octet class represents a Tuple with eight elements.

Class Declaration

以下 org.javatuples.Octet 类的声明 −

Following is the declaration for org.javatuples.Octet class −

public final class Octet<A, B, C, D, E, F, G, H>
   extends Tuple
      implements IValue0<A>, IValue1<B>,
         IValue2<C>, IValue3<D>, IValue4<E>,
            IValue5<F>, IValue6<G>, IValue7<H>

Class Constructor

Sr.No.

Constructor & Description

1

Octet(A value0, B value1, C value2, D value3, E value4, F value5, G value6, H value7) This creates a Octet Tuple.

Class Methods

Sr.No.

Method & Description

1

Ennead add(Unit tuple) This method returns a Ennead tuple. Similarly other methods to add tuples are available e.g. add(Pair tuple) returns Decade.

2

Ennead add(X0 value) This method add a value to the tuple and returns a Ennead tuple. Similarly other methods to add values are available e.g. add(X0 value0, X1 value1) returns Decade.

3

Ennead addAt0(Unit value) This method add a Unit tuple at index 0 and returns a Ennead tuple. Similarly other methods to add tuples are available e.g. addAt0(Pair value) returns Decade. Other similar method are addAt1(Unit value) which add a unit at index0 and have similar methods upto addAt7(Pair).

4

Ennead addAt0(X0 value) This method add a value at index 0 and returns a Ennead tuple. Similarly other methods to add values are available e.g. addAt0(X0 value0, X1 value1) returns Decade. Other similar method are addAt1(X0 value) which add a value at index0 and have similar methods upto addAt7() with two parameters.

5

static <X> Octet<X,X,X,X,X,X,X,X> fromArray(X[] array) Create tuple from array.

6

static <X> Octet<X,X,X,X,X,X,X,X> fromCollection(Collection<X> collection) Create tuple from collection.

7

static <X> Octet<X,X,X,X,X,X,X,X> fromIterable(Iterable<X> iterable) Create tuple from iterable.

8

static <X> Octet<X,X,X,X,X,X,X,X> fromIterable(Iterable<X> iterable, int index) Create tuple from iterable, starting from the specified index.

9

int getSize() Return the size of the tuple.

10

A getValue0() Returns the value of the tuple at index 0. Similarly getValue1() upto getValue7() returns the value at index 1 and so on.

11

Septet<B,C,D,E,F,G,H> removeFrom0() Return the tuple after removing value of the tuple at index 0. Similarly removeFrom1() upto removeFrom7() returns the tuple after removing value of the tuple at index 1 and so on.

12

<X> Octet<X,B,C,D,E,F,G,H> setAt0(X value) Set the value of the tuple at index 0.

13

static <A> Octet<A,B,C,D,E,F,G,H> with(A value0, B value1, C value2, D value3, E value4, F value5, G value6, H value7) Create the tuple using given value.

Methods inherite

此类从以下类中继承方法:

This class inherits methods from the following classes −

  1. org.javatuples.Tuple

  2. Object

Example

让我们看看 Octet 类如何运作。在此,我们将看到如何使用各种方法。

Let’s see Octet Class in action. Here we’ll see how to use various methods.

C:&gt;JavaTuples 中创建一个名为 TupleTester 的 Java 类文件。

Create a java class file named TupleTester in C:\>JavaTuples.

文件:TupleTester.java

File: TupleTester.java

package com.tutorialspoint;
import java.util.ArrayList;
import java.util.List;
import org.javatuples.Ennead;
import org.javatuples.Octet;
import org.javatuples.Septet;
public class TupleTester {
   public static void main(String args[]){
      Octet<Integer, Integer, Integer, Integer, Integer,Integer,Integer,Integer>
      octet = Octet.with(5, 6, 7,8,9,10,11,12);
      System.out.println(octet);
      boolean isPresent = octet.contains(5);
      System.out.println("5 is present: " + isPresent);
      List<Integer> list = new ArrayList<>();
      list.add(1);
      list.add(2);
      list.add(3);
      list.add(4);
      list.add(5);
      list.add(6);
      list.add(7);
      list.add(8);
      Ennead<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, String>
      ennead = octet.add("Test");
      System.out.println(ennead);
      Integer value = octet.getValue0();
      System.out.println(value);
      Septet<Integer, Integer, Integer, Integer,Integer, Integer,Integer>
      septet = octet.removeFrom0();
      System.out.println(septet);
      Octet<Integer, Integer, Integer, Integer, Integer,Integer, Integer, Integer>
      octet1 = Octet.fromCollection(list);
      System.out.println(octet1);
   }
}

Verify the result

Verify the result

使用以下 javac 编译器编译类:

Compile the classes using javac compiler as follows −

C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java

现在运行 TupleTester 查看结果:

Now run the TupleTester to see the result −

C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester

Output

验证输出

Verify the Output

[5, 6, 7, 8, 9, 10, 11, 12]
5 is present: true
[5, 6, 7, 8, 9, 10, 11, 12, Test]
5
[6, 7, 8, 9, 10, 11, 12]
[1, 2, 3, 4, 5, 6, 7, 8]

JavaTuples - Ennead Class

Introduction

org.javatuples.Ennead 类表示包含九个元素的元组。

The org.javatuples.Ennead class represents a Tuple with nine elements.

Class Declaration

以下 org.javatuples.Ennead 类的声明 −

Following is the declaration for org.javatuples.Ennead class −

public final class Ennead<A, B, C, D, E, F, G, H, I>
   extends Tuple
      implements IValue0<A>, IValue1<B>,
         IValue2<C>, IValue3<D>, IValue4<E>,
            IValue5<F>, IValue6<G>, IValue7<H>,
               IValue8<I>

Class Constructor

Sr.No.

Constructor & Description

1

Ennead(A value0, B value1, C value2, D value3, E value4, F value5, G value6, H value7, I value8) This creates a Ennead Tuple.

Class Methods

Sr.No.

Method & Description

1

Decade add(Unit tuple) This method returns a Decade tuple.

2

Decade add(X0 value) This method add a value to the tuple and returns a Decade tuple.

3

Decade addAt0(Unit value) This method add a Unit tuple at index 0 and returns a Decade tuple. Other similar method are addAt1(Unit value) which add a unit at index0 and have similar methods upto addAt8(Unit).

4

Decade addAt0(X0 value) This method add a value at index 0 and returns a Decade tuple. Other similar method are addAt1(X0 value) which add a value at index0 and have similar methods upto addAt8() with one parameter.

5

static <X> Ennead<X,X,X,X,X,X,X,X,X > fromArray(X[] array) Create tuple from array.

6

static <X> Ennead<X,X,X,X,X,X,X,X,X> fromCollection(Collection<X> collection) Create tuple from collection.

7

static <X> Ennead<X,X,X,X,X,X,X,X,X> fromIterable(Iterable<X> iterable) Create tuple from iterable.

8

static <X> Ennead<X,X,X,X,X,X,X,X> fromIterable(Iterable<X> iterable, int index) Create tuple from iterable, starting from the specified index.

9

int getSize() Return the size of the tuple.

10

A getValue0() Returns the value of the tuple at index 0. Similarly getValue1() upto getValue8() returns the value at index 1 and so on.

11

Octet<B,C,D,E,F,G,H,I> removeFrom0() Return the tuple after removing value of the tuple at index 0. Similarly removeFrom1() upto removeFrom8() returns the tuple after removing value of the tuple at index 1 and so on.

12

<X> Ennead<X,B,C,D,E,F,G,H,I> setAt0(X value) Set the value of the tuple at index 0.

13

static <A> Ennead<A,B,C,D,E,F,G,H,I> with(A value0, B value1, C value2, D value3, E value4, F value5, G value6, H value7, I value8) Create the tuple using given value.

Methods inherite

此类从以下类中继承方法:

This class inherits methods from the following classes −

  1. org.javatuples.Tuple

  2. Object

Example

让我们看看 Ennead 类的作用。我们将在其中看到如何使用各种方法。

Let’s see Ennead Class in action. Here we’ll see how to use various methods.

C:&gt;JavaTuples 中创建一个名为 TupleTester 的 Java 类文件。

Create a java class file named TupleTester in C:\>JavaTuples.

文件:TupleTester.java

File: TupleTester.java

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.List;
import org.javatuples.Decade;
import org.javatuples.Ennead;
import org.javatuples.Octet;
public class TupleTester {
   public static void main(String args[]){
      Ennead<Integer, Integer, Integer, Integer, Integer,
         Integer,Integer,Integer, Integer>
      ennead = Ennead.with(5, 6, 7,8,9,10,11,12,13);
      System.out.println(ennead);
      boolean isPresent = ennead.contains(5);
      System.out.println("5 is present: " + isPresent);
      List<Integer> list = new ArrayList<>();
      list.add(1);
      list.add(2);
      list.add(3);
      list.add(4);
      list.add(5);
      list.add(6);
      list.add(7);
      list.add(8);
      list.add(9);
      Decade<Integer, Integer, Integer, Integer, Integer,
         Integer, Integer, Integer, Integer, String> decade = ennead.add("Test");
      System.out.println(decade);
      Integer value = ennead.getValue0();
      System.out.println(value);
      Octet<Integer, Integer, Integer, Integer,Integer,
         Integer,Integer, Integer> octet = ennead.removeFrom0();
      System.out.println(octet);
      Ennead<Integer, Integer, Integer, Integer, Integer,
         Integer, Integer, Integer,Integer> ennead1 = Ennead.fromCollection(list);
      System.out.println(ennead1);
   }
}

Verify the result

Verify the result

使用以下 javac 编译器编译类:

Compile the classes using javac compiler as follows −

C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java

现在运行 TupleTester 查看结果:

Now run the TupleTester to see the result −

C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester

Output

验证输出

Verify the Output

[5, 6, 7, 8, 9, 10, 11, 12, 13]
5 is present: true
[5, 6, 7, 8, 9, 10, 11, 12, 13, Test]
5
[6, 7, 8, 9, 10, 11, 12, 13]
[1, 2, 3, 4, 5, 6, 7, 8, 9]

JavaTuples - Decade Class

Introduction

org.javatuples.Decade 类表示一个有十个元素的元组。

The org.javatuples.Decade class represents a Tuple with ten elements.

Class Declaration

以下是对 org.javatuples.Decade 类的声明 −

Following is the declaration for org.javatuples.Decade class −

public final class Decade<A, B, C, D, E, F, G, H, I, J>
   extends Tuple
      implements IValue0<A>, IValue1<B>,
         IValue2<C>, IValue3<D>, IValue4<E>,
            IValue5<F>, IValue6<G>, IValue7<H>,
               IValue8<I>, IValue9<J>

Class Constructor

Sr.No.

Constructor & Description

1

Decade(A value0, B value1, C value2, D value3, E value4, F value5, G value6, H value7, I value8, I value9 ) This creates a Decade Tuple.

Class Methods

Sr.No.

Method & Description

1

static <X> Decade<X,X,X,X,X,X,X,X,X,X > fromArray(X[] array) Create tuple from array.

2

static <X> Decade<X,X,X,X,X,X,X,X,X,X> fromCollection(Collection<X> collection) Create tuple from collection.

3

static <X> Decade<X,X,X,X,X,X,X,X,X,X> fromIterable(Iterable<X> iterable) Create tuple from iterable.

4

static <X> Decade<X,X,X,X,X,X,X,X,X> fromIterable(Iterable<X> iterable, int index) Create tuple from iterable, starting from the specified index.

5

int getSize() Return the size of the tuple.

6

A getValue0() Returns the value of the tuple at index 0. Similarly getValue1() upto getValue9() returns the value at index 1 and so on.

7

Ennead<B,C,D,E,F,G,H,I,J> removeFrom0() Return the tuple after removing value of the tuple at index 0. Similarly removeFrom1() upto removeFrom9() returns the tuple after removing value of the tuple at index 1 and so on.

8

<X> Decade<X,B,C,D,E,F,G,H,I,J> setAt0(X value) Set the value of the tuple at index 0.

9

static <A> Decade<A,B,C,D,E,F,G,H,I,J> with(A value0, B value1, C value2, D value3, E value4, F value5, G value6, H value7, I value8, I value9) Create the tuple using given value.

Methods inherite

此类从以下类中继承方法:

This class inherits methods from the following classes −

  1. org.javatuples.Tuple

  2. Object

Example

让我们看看 Ennead 类的作用。我们将在其中看到如何使用各种方法。

Let’s see Ennead Class in action. Here we’ll see how to use various methods.

C:&gt;JavaTuples 中创建一个名为 TupleTester 的 Java 类文件。

Create a java class file named TupleTester in C:\>JavaTuples.

文件:TupleTester.java

File: TupleTester.java

package com.tutorialspoint;
import java.util.ArrayList;
import java.util.List;
import org.javatuples.Decade;
import org.javatuples.Ennead;
public class TupleTester {
   public static void main(String args[]){
      Decade<Integer, Integer, Integer, Integer,
         Integer,Integer,Integer,Integer, Integer, Integer>
      decade = Decade.with(5, 6, 7,8,9,10,11,12,13,14);
      System.out.println(decade);
      boolean isPresent = decade.contains(5);
      System.out.println("5 is present: " + isPresent);
      List<Integer> list = new ArrayList<>();
      list.add(1);
      list.add(2);
      list.add(3);
      list.add(4);
      list.add(5);
      list.add(6);
      list.add(7);
      list.add(8);
      list.add(9);
      list.add(10);
      Integer value = decade.getValue0();
      System.out.println(value);
      Ennead<Integer, Integer, Integer, Integer,Integer,
         Integer,Integer, Integer, Integer> ennead = decade.removeFrom0();
      System.out.println(ennead);
      Decade<Integer, Integer, Integer, Integer, Integer,
         Integer, Integer, Integer,Integer, Integer>
         decade1 = Decade.fromCollection(list);
      System.out.println(decade1);
   }
}

Verify the result

Verify the result

使用以下 javac 编译器编译类:

Compile the classes using javac compiler as follows −

C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java

现在运行 TupleTester 查看结果:

Now run the TupleTester to see the result −

C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester

Output

验证输出

Verify the Output

[5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
5 is present: true
5
[6, 7, 8, 9, 10, 11, 12, 13, 14]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

JavaTuples - LabelValues Class

Introduction

org.javatuples.LabelValue 类表示一个元组,其两个元素置为 0 和 1 的位置,分别重命名为“label”和“value”。

The org.javatuples.LabelValue class represents a Tuple with two elements with positions 0 and 1 renamed as "label" and "value", respectively.

Class Declaration

以下是 org.javatuples.LabelValue 类的声明:

Following is the declaration for org.javatuples.LabelValue class −

public final class LabelValue<A,B>
   extends Tuple
      implements IValue0<A>, IValue1<B>

Class Constructor

Sr.No.

Constructor & Description

1

LabelValue(A value0, B value1) This creates a LabelValue Tuple.

Class Methods

Sr.No.

Method & Description

1

static <X> LabelValue<X,X> fromArray(X[] array) Create tuple from array.

2

static <X> LabelValue<X,X> fromCollection(Collection<X> collection) Create tuple from collection.

3

static <X> LabelValue<X,X> fromIterable(Iterable<X> iterable) Create tuple from iterable.

4

static <X> LabelValue<X,X> fromIterable(Iterable<X> iterable, int index) Create tuple from iterable, starting from the specified index.

5

A getLabel() Return the label.

6

int getSize() Return the size of the tuple.

7

A getValue() Returns the value of the tuple.

8

<X> LabelValue<X,B> setLabel(X label) set the label and return the tuple.

9

<X> LabelValue<A,Y> setValue(Y value) set the value and return the tuple.

10

static <A,B> LabelValue<A,B> with(A value0, B value1) Create the tuple using given value.

Methods inherits

此类从以下类中继承方法:

This class inherits methods from the following classes −

  1. org.javatuples.Tuple

  2. Object

Example

我们来看一下 LabelValue 类的实际应用。在这里,我们将看到如何使用各种方法。

Let’s see LabelValue Class in action. Here we’ll see how to use various methods.

C:&gt;JavaTuples 中创建一个名为 TupleTester 的 Java 类文件。

Create a java class file named TupleTester in C:\>JavaTuples.

文件:TupleTester.java

File: TupleTester.java

package com.tutorialspoint;
import java.util.ArrayList;
import java.util.List;
import org.javatuples.LabelValue;
public class TupleTester {
   public static void main(String args[]){
      LabelValue<Integer, Integer> labelValue = LabelValue.with(5,6);
      System.out.println(labelValue);
      List<Integer> list = new ArrayList<>();
      list.add(1);
      list.add(2);
      Integer label = labelValue.getLabel();
      System.out.println(label);
      Integer value = labelValue.getValue();
      System.out.println(value);
      LabelValue<Integer, Integer> labelValue1
         = LabelValue.fromCollection(list);
      System.out.println(labelValue1);
   }
}

Verify the result

Verify the result

使用以下 javac 编译器编译类:

Compile the classes using javac compiler as follows −

C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java

现在运行 TupleTester 查看结果:

Now run the TupleTester to see the result −

C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester

Output

验证输出

Verify the Output

[5, 6]
5
6
[1, 2]

JavaTuples - KeyValue Class

Introduction

org.javatuples.KeyValue 类表示一个元组,其两个元素置为 0 和 1 的位置,分别重命名为“key”和“value”。

The org.javatuples.KeyValue class represents a Tuple with two elements with positions 0 and 1 renamed as "key" and "value", respectively.

Class Declaration

以下是 org.javatuples.KeyValue 类的声明 −

Following is the declaration for org.javatuples.KeyValue class −

public final class KeyValue<A,B>
   extends Tuple
      implements IValue0<A>, IValue1<B>

Class Constructor

Sr.No.

Constructor & Description

1

KeyValue(A value0, B value1) This creates a KeyValue Tuple.

Class Methods

Sr.No.

Method & Description

1

static <X> KeyValue<X,X> fromArray(X[] array) Create tuple from array.

2

static <X> KeyValue<X,X> fromCollection(Collection<X> collection) Create tuple from collection.

3

static <X> KeyValue<X,X> fromIterable(Iterable<X> iterable) Create tuple from iterable.

4

static <X> KeyValue<X,X> fromIterable(Iterable<X> iterable, int index) Create tuple from iterable, starting from the specified index.

5

A getKey() Return the key.

6

int getSize() Return the size of the tuple.

7

A getValue() Returns the value of the tuple.

8

<X> KeyValue<X,B> setKey(X key) set the label and return the tuple.

9

<X> KeyValue<A,Y> setValue(Y value) set the value and return the tuple.

10

static <A,B> KeyValue<A,B> with(A value0, B value1) Create the tuple using given value.

Methods inherite

此类从以下类中继承方法:

This class inherits methods from the following classes −

  1. org.javatuples.Tuple

  2. Object

Example

让我们看看 KeyValue 类的实际操作。我们将在其中了解如何使用各种方法。

Let’s see KeyValue Class in action. Here we’ll see how to use various methods.

C:&gt;JavaTuples 中创建一个名为 TupleTester 的 Java 类文件。

Create a java class file named TupleTester in C:\>JavaTuples.

文件:TupleTester.java

File: TupleTester.java

package com.tutorialspoint;
import java.util.ArrayList;
import java.util.List;
import org.javatuples.KeyValue;
public class TupleTester {
   public static void main(String args[]){
      KeyValue<Integer, Integer> keyValue = KeyValue.with(5,6);
      System.out.println(keyValue);
      List<Integer> list = new ArrayList<>();
      list.add(1);
      list.add(2);
      Integer key = KeyValue.getKey();
      System.out.println(key);
      Integer value = KeyValue.getValue();
      System.out.println(value);
      KeyValue<Integer, Integer> keyValue1 = KeyValue.fromCollection(list);
      System.out.println(keyValue1);
   }
}

Verify the result

Verify the result

使用以下 javac 编译器编译类:

Compile the classes using javac compiler as follows −

C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java

现在运行 TupleTester 查看结果:

Now run the TupleTester to see the result −

C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester

Output

验证输出

Verify the Output

[5, 6]
5
6
[1, 2]

Implementing Pair Using Unit Class

Problem Description

如何使用 Unit 类实现 Pair 类?

How to implement Pair class using Unit class?

Example

以下示例描述了如何完成以上任务。每个元组都有 add() 和 addAtX() 方法来转换元组。

Following example shows how to accomplish the above task. Each tuple has add() and addAtX() methods to convert the tuple.

C:&gt;JavaTuples 中创建一个名为 TupleTester 的 Java 类文件。

Create a java class file named TupleTester in C:\>JavaTuples.

文件:TupleTester.java

File: TupleTester.java

package com.tutorialspoint;
import org.javatuples.Pair;
import org.javatuples.Unit;
public class TupleTester {
   public static void main(String args[]){
      Unit<Integer> unit = Unit.with(5);
      System.out.println(unit);
      Pair<Integer, String> pair = unit.add("test");
      Pair<String, Integer> pair1 = unit.addAt0("test");
      System.out.println(pair);
      System.out.println(pair1);
   }
}

Verify the result

Verify the result

使用以下 javac 编译器编译类:

Compile the classes using javac compiler as follows −

C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java

现在运行 TupleTester 查看结果:

Now run the TupleTester to see the result −

C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester

Output

验证输出

Verify the Output

[5]
[5, test]
[test, 5]

Implementing Triplet Using Pair Class

Problem Description

如何使用 Pair 类实现 Triplet 类?

How to implement Triplet class using Pair class?

Example

以下示例描述了如何完成以上任务。每个元组都有 add() 和 addAtX() 方法来转换元组。

Following example shows how to accomplish the above task. Each tuple has add() and addAtX() methods to convert the tuple.

C:&gt;JavaTuples 中创建一个名为 TupleTester 的 Java 类文件。

Create a java class file named TupleTester in C:\>JavaTuples.

文件:TupleTester.java

File: TupleTester.java

package com.tutorialspoint;
import org.javatuples.Triplet;
import org.javatuples.Pair;
public class TupleTester {
   public static void main(String args[]){
      Pair<Integer, Integer> pair = Pair.with(5,6);
      System.out.println(pair);
      Triplet<Integer, Integer, String> triplet = pair.add("test");
      Triplet<String, Integer, Integer> triplet1 = pair.addAt0("test");
      System.out.println(triplet);
      System.out.println(triplet1);
   }
}

Verify the result

Verify the result

使用以下 javac 编译器编译类:

Compile the classes using javac compiler as follows −

C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java

现在运行 TupleTester 查看结果:

Now run the TupleTester to see the result −

C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester

Output

验证输出

Verify the Output

[5, 6]
[5, 6, test]
[test, 5, 6]

Implementing Quartet Using Triplet Class

Problem Description

如何使用 Triplet 类实现 Quartet 类?

How to implement Quartet class using Triplet class?

Example

以下示例描述了如何完成以上任务。每个元组都有 add() 和 addAtX() 方法来转换元组。

Following example shows how to accomplish the above task. Each tuple has add() and addAtX() methods to convert the tuple.

C:&gt;JavaTuples 中创建一个名为 TupleTester 的 Java 类文件。

Create a java class file named TupleTester in C:\>JavaTuples.

文件:TupleTester.java

File: TupleTester.java

package com.tutorialspoint;
import org.javatuples.Quartet;
import org.javatuples.Triplet;
public class TupleTester {
   public static void main(String args[]){
      Triplet<Integer, Integer, Integer> triplet = Triplet.with(5,6,7);
      System.out.println(triplet);
      Quartet<Integer, Integer, Integer, String> quartet = triplet.add("test");
      Quartet<String, Integer, Integer, Integer> quartet1 = triplet.addAt0("test");
      System.out.println(quartet);
      System.out.println(quartet1);
   }
}

Verify the result

Verify the result

使用以下 javac 编译器编译类:

Compile the classes using javac compiler as follows −

C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java

现在运行 TupleTester 查看结果:

Now run the TupleTester to see the result −

C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester

Output

验证输出

Verify the Output

[5, 6, 7]
[5, 6, 7, test]
[test, 5, 6, 7]

Implementing Quintet Using Quartet Class

Problem Description

如何使用 Quartet 类来实现 Quintet 类?

How to implement Quintet class using Quartet class?

Example

以下示例描述了如何完成以上任务。每个元组都有 add() 和 addAtX() 方法来转换元组。

Following example shows how to accomplish the above task. Each tuple has add() and addAtX() methods to convert the tuple.

C:&gt;JavaTuples 中创建一个名为 TupleTester 的 Java 类文件。

Create a java class file named TupleTester in C:\>JavaTuples.

文件:TupleTester.java

File: TupleTester.java

package com.tutorialspoint;
import org.javatuples.Quintet;
import org.javatuples.Quartet;
public class TupleTester {
   public static void main(String args[]){
      Quartet<Integer, Integer, Integer, Integer> quartet = Quartet.with(5,6,7,8);
      System.out.println(quartet);
      Quintet<Integer, Integer, Integer, Integer, String> quintet = quartet.add("test");
      Quintet<String, Integer, Integer, Integer, Integer> quintet1 = quartet.addAt0("test");
      System.out.println(quintet);
      System.out.println(quintet1);
   }
}

Verify the result

Verify the result

使用以下 javac 编译器编译类:

Compile the classes using javac compiler as follows −

C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java

现在运行 TupleTester 查看结果:

Now run the TupleTester to see the result −

C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester

Output

验证输出

Verify the Output

[5, 6, 7, 8]
[5, 6, 7, 8, test]
[test, 5, 6, 7, 8]

Implementing Sextet Using Quintet Class

Problem Description

如何使用 Quintet 类实现 Sextet 类?

How to implement Sextet class using Quintet class?

Example

以下示例描述了如何完成以上任务。每个元组都有 add() 和 addAtX() 方法来转换元组。

Following example shows how to accomplish the above task. Each tuple has add() and addAtX() methods to convert the tuple.

C:&gt;JavaTuples 中创建一个名为 TupleTester 的 Java 类文件。

Create a java class file named TupleTester in C:\>JavaTuples.

文件:TupleTester.java

File: TupleTester.java

package com.tutorialspoint;
import org.javatuples.Quintet;
import org.javatuples.Sextet;
public class TupleTester {
   public static void main(String args[]){
      Quintet<Integer, Integer, Integer, Integer, Integer> quintet
         = Quintet.with(5,6,7,8,9);
      System.out.println(quintet);
      Sextet<Integer, Integer, Integer, Integer, Integer, String> sextet
         = quintet.add("test");
      Sextet<String, Integer, Integer, Integer, Integer, Integer> sextet1
         = quintet.addAt0("test");
      System.out.println(sextet);
      System.out.println(sextet1);
   }
}

Verify the result

Verify the result

使用以下 javac 编译器编译类:

Compile the classes using javac compiler as follows −

C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java

现在运行 TupleTester 查看结果:

Now run the TupleTester to see the result −

C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester

Output

验证输出

Verify the Output

[5, 6, 7, 8, 9]
[5, 6, 7, 8, 9, test]
[test, 5, 6, 7, 8, 9]

Implementing Septet using Sextet Class

Problem Description

如何使用 Sextet 类实现 Septet 类?

How to implement Septet class using Sextet class?

Example

以下示例描述了如何完成以上任务。每个元组都有 add() 和 addAtX() 方法来转换元组。

Following example shows how to accomplish the above task. Each tuple has add() and addAtX() methods to convert the tuple.

C:&gt;JavaTuples 中创建一个名为 TupleTester 的 Java 类文件。

Create a java class file named TupleTester in C:\>JavaTuples.

文件:TupleTester.java

File: TupleTester.java

package com.tutorialspoint;
import org.javatuples.Septet;
import org.javatuples.Sextet;
public class TupleTester {
   public static void main(String args[]){
      Sextet<Integer, Integer, Integer, Integer, Integer, Integer> sextet
         = Sextet.with(5,6,7,8,9,10);
      System.out.println(sextet);
      Septet<Integer, Integer, Integer, Integer, Integer, Integer, String>
         septet = sextet.add("test");
      Septet<String, Integer, Integer, Integer, Integer, Integer, Integer>
         septet1 = sextet.addAt0("test");
      System.out.println(septet);
      System.out.println(septet1);
   }
}

Verify the result

Verify the result

使用以下 javac 编译器编译类:

Compile the classes using javac compiler as follows −

C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java

现在运行 TupleTester 查看结果:

Now run the TupleTester to see the result −

C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester

Output

验证输出

Verify the Output

[5, 6, 7, 8, 9, 10]
[5, 6, 7, 8, 9, 10, test]
[test, 5, 6, 7, 8, 9, 10]

Implementing Octet using Septet Class

Problem Description

如何使用 Septet 类实现 Octet 类?

How to implement Octet class using Septet class?

Example

以下示例描述了如何完成以上任务。每个元组都有 add() 和 addAtX() 方法来转换元组。

Following example shows how to accomplish the above task. Each tuple has add() and addAtX() methods to convert the tuple.

C:&gt;JavaTuples 中创建一个名为 TupleTester 的 Java 类文件。

Create a java class file named TupleTester in C:\>JavaTuples.

文件:TupleTester.java

File: TupleTester.java

package com.tutorialspoint;
import org.javatuples.Octet;
import org.javatuples.Septet;
public class TupleTester {
   public static void main(String args[]){
      Septet<Integer, Integer, Integer, Integer, Integer, Integer,
         Integer> septet = Septet.with(5,6,7,8,9,10,11);
      System.out.println(septet);
      Octet<Integer, Integer, Integer, Integer, Integer, Integer,
         Integer, String> octet = septet.add("test");
      Octet<String, Integer, Integer, Integer, Integer, Integer,
         Integer, Integer> octet1 = septet.addAt0("test");
      System.out.println(octet);
      System.out.println(octet1);
   }
}

Verify the result

Verify the result

使用以下 javac 编译器编译类:

Compile the classes using javac compiler as follows −

C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java

现在运行 TupleTester 查看结果:

Now run the TupleTester to see the result −

C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester

Output

验证输出

Verify the Output

[5, 6, 7, 8, 9, 10, 11]
[5, 6, 7, 8, 9, 10, 11, test]
[test, 5, 6, 7, 8, 9, 10, 11]

Implementing Ennead Using Octet Class

Problem Description

如何使用 Octet 类实现 Ennead 类?

How to implement Ennead class using Octet class?

Example

以下示例描述了如何完成以上任务。每个元组都有 add() 和 addAtX() 方法来转换元组。

Following example shows how to accomplish the above task. Each tuple has add() and addAtX() methods to convert the tuple.

C:&gt;JavaTuples 中创建一个名为 TupleTester 的 Java 类文件。

Create a java class file named TupleTester in C:\>JavaTuples.

文件:TupleTester.java

File: TupleTester.java

package com.tutorialspoint;
import org.javatuples.Ennead;
import org.javatuples.Octet;
public class TupleTester {
   public static void main(String args[]){
      Octet<Integer, Integer, Integer, Integer, Integer, Integer,
         Integer, Integer> octet = Octet.with(5,6,7,8,9,10,11,12);
      System.out.println(octet);
      Ennead<Integer, Integer, Integer, Integer, Integer, Integer,
         Integer, Integer, String> ennead = octet.add("test");
      Ennead<String, Integer, Integer, Integer, Integer, Integer,
         Integer, Integer, Integer> ennead1 = octet.addAt0("test");
      System.out.println(ennead);
      System.out.println(ennead1);
   }
}

Verify the result

Verify the result

使用以下 javac 编译器编译类:

Compile the classes using javac compiler as follows −

C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java

现在运行 TupleTester 查看结果:

Now run the TupleTester to see the result −

C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester

Output

验证输出

Verify the Output

[5, 6, 7, 8, 9, 10, 11, 12]
[5, 6, 7, 8, 9, 10, 11, 12, test]
[test, 5, 6, 7, 8, 9, 10, 11, 12]

Implementing Decade Using Ennead Class

Problem Description

如何使用 Ennead 类实现 Decade 类?

How to implement Decade class using Ennead class?

Example

以下示例描述了如何完成以上任务。每个元组都有 add() 和 addAtX() 方法来转换元组。

Following example shows how to accomplish the above task. Each tuple has add() and addAtX() methods to convert the tuple.

C:&gt;JavaTuples 中创建一个名为 TupleTester 的 Java 类文件。

Create a java class file named TupleTester in C:\>JavaTuples.

文件:TupleTester.java

File: TupleTester.java

package com.tutorialspoint;
import org.javatuples.Decade;
import org.javatuples.Ennead;
public class TupleTester {
   public static void main(String args[]){
      Ennead<Integer, Integer, Integer, Integer, Integer, Integer,
         Integer, Integer, Integer> ennead = Ennead.with(5,6,7,8,9,10,11,12,13);
      System.out.println(ennead);
      Decade<Integer, Integer, Integer, Integer, Integer, Integer,
         Integer, Integer, Integer, String> decade = ennead.add("test");

      Decade<String, Integer, Integer, Integer, Integer, Integer,
         Integer, Integer, Integer, Integer> decade1 = ennead.addAt0("test");

      System.out.println(decade);
      System.out.println(decade1);
   }
}

Verify the result

Verify the result

使用以下 javac 编译器编译类:

Compile the classes using javac compiler as follows −

C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java

现在运行 TupleTester 查看结果:

Now run the TupleTester to see the result −

C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester

Output

验证输出

Verify the Output

[5, 6, 7, 8, 9, 10, 11, 12, 13]
[5, 6, 7, 8, 9, 10, 11, 12, 13, test]
[test, 5, 6, 7, 8, 9, 10, 11, 12, 13]